The example in this article tells you how to scan the directory for the Go language and get relevant information. Share to everyone for your reference. The specific analysis is as follows:
Foreword: Recently saw in Go inside has a func to be able to scan the entire catalog easily, and can obtain the corresponding directory and the file information, therefore I have carried on the encapsulation, obtains the file info all information to be able to do other uses conveniently.
Direct code, code based on GO version 1
Copy Code code as follows:
Package Main
Import (
"Path/filepath"
"OS"
"Flag"
"FMT"
"Time"
)
Const (
Isdirectory = Iota
Isregular
Issymlink
)
Type sysfile struct {
FType int
FName string
Flink string
Fsize Int64
Fmtime time. Time
Fperm OS. FileMode
}
Type F struct {
files []*sysfile
}
Func (self *f) visit (Path string, F OS. FileInfo, err Error) error {
if (f = = nil) {
return err
}
var TP int
If F.isdir () {
TP = Isdirectory
}else if (F.mode () & OS. Modesymlink) > 0 {
TP = Issymlink
}else{
TP = Isregular
}
Inofile: = &sysfile{
Fname:path,
FTYPE:TP,
Fperm:f.mode (),
Fmtime:f.modtime (),
Fsize:f.size (),
}
Self.files = Append (Self.files, inofile)
return Nil
}
Func Main () {
Flag. Parse ()
Root: = flag. ARG (0)
Self: = f{
Files:make ([]*sysfile, 0),
}
ERR: = FilePath. Walk (Root, func (path string, f OS). FileInfo, err Error) error {
return Self.visit (path, F, err)
})
If Err!= nil {
Fmt. Printf ("filepath. Walk () returned%v\n ", err)
}
For _, V: = Range Self.files {
Fmt. Println (V.fname,v.fsize)
}
}
I hope this article will help you with your go language program.