dongzhouji4021 2019-06-03 20:55
浏览 952
已采纳

golang将os.ModePerm转换为字符串

I would like to have as a string the permissions representation of a file. Here is what I want to do :

fileInfo, err := os.Lstat(path)
fileMode := fileInfo.Mode()
// fileMode.String() gives dturwxrwxrwx or -rwxrwxrwx
// which i do not want because the size is not always the same
unixPerms := fileMode & os.ModePerm

I get -rwxrwxrwx for both cases, which is close to what i am looking for.

However, the returned object is of type os.FileMode. How can I then convert it into a string?

  • 写回答

2条回答 默认 最新

  • download92000 2019-06-03 21:03
    关注

    You can pass either one of your variables, type (os.FileMode), as an argument to the Sprintf method from the fmt package.

    Leveraging this method will convert your type into a string, which can then be used as a string throughout the remainder of your program.

    Example usage below:

    package main
    
    import (
        "fmt"
        "os"
    )
    
    func main() {
        fileInfo, err := os.Lstat(path)
        if err != nil {
            // catch err
        }
        fileMode := fileInfo.Mode()
        // fileMode.String() gives dturwxrwxrwx or -rwxrwxrwx
        // which i do not want because the size is not always the same
        unixPerms := fileMode & os.ModePerm
    
        permString := fmt.Sprintf("%v", unixPerms)
        fmt.Println(permString)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 提问应符合社区要求 12月14日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度