我该如何在Go语言中编写一个函数来检查文件是否可执行?给定一个os.FileInfo
,我可以获取os.FileInfo.Mode()
,但我会在尝试解析权限位时陷入困境。
测试用例:
#!/usr/bin/env bash
function setup() {
mkdir -p test/foo/bar
touch test/foo/bar/{baz.txt,quux.sh}
chmod +x test/foo/bar/quux.sh
}
function teardown() { rm -r ./test }
setup
import (
"os"
"path/filepath"
"fmt"
)
func IsExectuable(mode os.FileMode) bool {
// ???
}
func main() {
filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
fmt.Printf("%v %v", path, IsExectuable(info.Mode().Perm()))
}
}
// should print "test/foo/bar/baz.txt false"
// "test/foo/bar/quux.txt true"
我只关心Unix文件,如果解决方案也适用于Windows系统,则加分。