1. 函数原型
func Stat(name string) (FileInfo, error)
2. FileInfo数据结构
// A FileInfo describes a file and is returned by Stat and Lstat.type FileInfo interface {Name() string // base name of the fileSize() int64 // length in bytes for regular files; system-dependent for othersMode() FileMode // file mode bitsModTime() time.Time // modification timeIsDir() bool // abbreviation for Mode().IsDir()Sys() interface{} // underlying data source (can return nil)}
3. 应用举例
package mainimport (\"fmt\"\"os\")func main() {list := os.Argsif len(list) != 2 {fmt.Println(\"Usage: .\\\\main.exe file\")return}fileName := list[1]info, err := os.Stat(fileName)if err != nil {fmt.Println(\"get fileInfo failed:\", err)return}fmt.Println(\"name = \", info.Name())fmt.Println(\"size = \", info.Size())}/*output:PS E:\\Code\\GoCode\\GetFileAttr> go run .\\main.go .\\go.modname = go.modsize = 28PS E:\\Code\\GoCode\\GetFileAttr> go build .\\main.goPS E:\\Code\\GoCode\\GetFileAttr> .\\main.exe main.goname = main.gosize = 369*/