Go file operations
Go provides support for file operations in multiple packages. Now I have learned this part, so I will record the usage of this package.
Package mainimport ("FMT" "OS" "Io") func main () {Fi, err: = OS. Open ("Main. Go") if Err! = Nil {FMT. println (ERR) return} WI, err: = OS. Create ("Wo. Go") if Err! = Nil {FMT. println (ERR) return} data: = make ([] bytes, 100) for {n, err: = Fi. Read (data) If Err! = Nil {If err = Io. EOF {break // read finished} else {FMT. println (ERR) }}w, err: = WI. write (data [0: N]) If Err! = Nil {FMT. println (ERR) // return} If W! = N {FMT. println ("it is also wrong! ") Return} FMT. Print (string (data [0: N])}
Get Object Attributes
package mainimport ( "fmt" "os")func main(){ fileInfo, err := os.Stat("main.go") if err != nil{ fmt.Println(err) } fmt.Print(fileInfo)}
The structure of fileinfo is as follows:
Type fileinfo interface {name () string // base name of the file name size () int64 // length in bytes for regular files; system-dependent for others mode () filemode // file mode bits modtime () time. time // modification time isdir () bool // abbreviation for mode (). isdir () sys () interface {}// underlying data source (can return nil )}
Go file operations