The go language scans the directory and obtains the related information the method _golang

Source: Internet
Author: User
Tags file info

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.