在Go中,可以使用os.Stat函数来检查文件是否可执行。具体步骤如下:
os包:import "os"使用os.Stat函数获取文件的信息:info, err := os.Stat("文件路径")检查文件的权限位是否包含可执行权限位:isExecutable := info.Mode().Perm()&0100 != 0完整示例代码如下:
package mainimport ("fmt""os")func main() {filePath := "path/to/your/file"info, err := os.Stat(filePath)if err != nil {fmt.Println("Error:", err)return}isExecutable := info.Mode().Perm()&0100 != 0if isExecutable {fmt.Println(filePath, "is executable")} else {fmt.Println(filePath, "is not executable")}}在上面的示例中,我们首先使用os.Stat函数获取文件的信息,然后使用info.Mode().Perm()获取文件的权限位,再通过&0100来检查文件是否包含可执行权限位。如果文件包含可执行权限位,即isExecutable为true,则表示文件可执行;否则表示文件不可执行。


