如何将filemode转换为int?
Sample code:
func main() {
p, _ := os.Open(os.Args[1])
m, _ := p.Stat()
println(m.Mode().Perm())
}
File has mode 0775
(-rwxrwxr-x
). Running it like:
./main main
Prints 509
And second:
func main() {
p, _ := os.Open(os.Args[1])
m, _ := p.Stat()
println(m.Mode().Perm().String())
}
This code prints -rwxrwxr-x
.
How I can get mode in format 0775
?
示例代码: p>
func main(){
p ,_:= os.Open(os.Args [1])
m,_:= p.Stat()
println(m.Mode()。Perm())
}
code>
文件的模式为 0775 code>( -rwxrwxr-x code>)。 像这样运行它: p>
./ main main p>
blockquote>
打印 509 code> p>
第二个: p>
func main(){
p,_:= os.Open(os.Args [1])
m,_:= p.Stat()
println(m.Mode()。Perm()。String())
}
code> pre>
代码打印 -rwxrwxr-x code>。 p>
如何获取 0775 code>格式的模式? p>
div >
The value 509
is the decimal (base 10) representation of the permission bits.
The form 0775
is the octal representation (base 8). You can print a number in octal representation using the %o
verb:
perm := 509
fmt.Printf("%o", perm)
Output (try it on the Go Playground):
775
If you want the output to be 4 digits (with a leading 0
in this case), use the format string "%04o"
:
fmt.Printf("%04o", perm) // Output: 0775