Automated management of files and folders
Output files and folders
Gets the root path of the current working directory
Package name: os
Prototype: func Getwd()(pwd string, err error)
Function: get the current file path
Return: string of current file path and an err message
Example:
package main import ( "fmt" "os" ) func main() { dir, _ := os.Getwd() fmt.Println("Current path:", dir) }
Output:
Current path: D:\Projects\Go\mGoLab01
Combined path
Package name: os
Prototype: func join (elem... String) string
Function: put any number of path elements into a single path, and slashes will be added as needed. The result is simplified, and all empty string elements are ignored.
Return: path after combination
Example:
package main import ( "fmt" "path" ) func main() { out := path.Join("a", "b", "c") fmt.Println("Combined path:", out) }
Output:
Combined path: a/b/c
traverse folder
Package name: os
Prototype: func Open(name string) (file *File, err error)
Function: Open opens a file for reading. If the operation is successful, the method of the returned file object can be used to read data; The corresponding file descriptor has O_RDONLY mode. If an error occurs, the underlying type of the error is * PathError.
Returning: file objects
Package name: os
Prototype: func (f *File) Readdir(n int) (fi []FileInfo, err error)
Function: Readdir reads the contents of directory f and returns a [] FileInfo with n members. These fileinfos are returned by Lstat in directory order. The next call to this function will return the information of the remaining unread content of the previous call.
If n > 0, the Readdir function returns a slice of up to n members. At this time, if Readdir returns an empty slice, it will return a non nil error stating the reason. If the end of directory f is reached, the return value err will be io EOF.
If n < = 0, the Readdir function returns the slice composed of FileInfo of all remaining file objects in the directory. At this point, if the Readdir call succeeds (reading everything until the end), it will return the error value of the slice and nil. If an error is encountered before reaching the end, the slice composed of FileInfo successfully read before and the error will be returned.
Returning: file objects
Example:
package main import ( "fmt" "os" ) func main() { f, err := os.Open(`F:\blog\officeauto`) if err != nil { panic(err) } fs, err := f.Readdir(0) if err != nil { panic(err) } for _, f := range fs { fmt.Println(f.Name()) } }
Package name: io/ioutil
Prototype: func ReadDir(dirname string) ([]os.FileInfo, error)
Function: returns an ordered list of directory information of the directory specified by dirname.
Returning: file objects
Example:
package main import ( "fmt" "io/ioutil" ) func main() { fs, err := ioutil.ReadDir(`F:\blog\officeauto`) if err != nil { panic(err) } for _, f := range fs { fmt.Println(f.Name()) } }
Determine whether the file is a directory
Package name: os
Prototype: func (f *File) Stat() (fi FileInfo, err error)
Function: Stat returns the FileInfo type value of description file f. If an error occurs, the underlying type of the error is * PathError.
Returning: file objects
Package name: os
Prototype: func (m FileMode) IsDir() bool
Function: IsDir reports whether m is a directory.
Return: bool
Example:
package main import ( "fmt" "os" ) func main() { s, _ := os.Stat(`F:\blog\officeauto`) fmt.Println(s.IsDir()) fmt.Println(s.Mode().IsDir()) }
Output:
true true
Judge whether the file is a file
Package name: os
Prototype: func (m FileMode) IsRegular() bool
Function: IsRegular reports whether m is an ordinary file.
Return: bool
Example:
package main import ( "fmt" "os" ) func main() { s, _ := os.Stat(`F:\blog\officeauto`) fmt.Println(s.Mode().IsRegular()) }
Output:
false