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

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日

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题