This is a creation in Article, where the information may have evolved or changed.
Read and write files are used in the OS package
stringinterror
The first parameter of the method is a file path, the second parameter controls how the file is opened, and the third parameter controls the file mode
The available open methods are
Const (//Read-only mode o_rdonlyint=Syscall. O_rdonly//OpenThe fileRead-only. Write-only mode o_wronlyint=Syscall. O_wronly//OpenThe fileWrite-only. Readable writable O_rdwrint=Syscall. O_RDWR//OpenThe fileRead-Write. Append content O_appendint=Syscall. O_append//APPEND data to the file whenWriting. Create a file if the file does not exist o_createint=Syscall. O_creat//Create a new fileifNoneexists. Used in conjunction with the Create file, the file must exist O_EXCLint=Syscall. O_EXCL//Used with o_create, file must notexist//open a synchronized file stream O_syncint=Syscall. O_sync//Open forSynchronous I/O. If possible, shorten the file O_trunc when openedint=Syscall. O_trunc//ifPossibletruncateFile whenOpened.)
Open mode
Const(// The single letters is the abbreviations //used by the String method ' s formatting.Modedir FileMode =1<<( +-1-Iota)//D:is a directory folder modeModeappend//a:append-only Append modeModeexclusive//l:exclusive use aloneModetemporary//T:temporary file (not backed up) temporary filesModesymlink//l:symbolic Link Symbolic AssociationModedevice//D:device file device filesModenamedpipe//P:named pipe (FIFO) Named PipesModesocket//S:unix domain socket UNIX host SocketModesetuid//U:setuid set UIDModesetgid//G:setgid set GIDModechardevice//C:unix character device, when Modedevice is set UNIX character device, when device mode is set UNIXModesticky//T:sticky sticky //Mask for the type bits. For regular files, none would be set. Bit bit obscured. unchanging file set to noneModetype = Modedir | Modesymlink | Modenamedpipe | Modesocket | Modedevice Modeperm FileMode =0777 //Unix permission bits permission bit.)
Create a file and append content
package mainimport ( "os")func main() { "/home/stack/tmp/t.txt" osos.O_CREATE|os.O_RDWR|osos.ModeAppend|os.ModePerm) ifnil { fmt.Println(err) } f.WriteString("test") f.Close()}
deleting files
os.Remove(fname)
Create a Directory
"/home/stack/tmp/d" osos.ModeDir|os.ModePerm)
Create a full directory path, that is, if the intermediate directory does not exist
os.MkdirAll(dname,os.ModeDir|os.ModePerm)
Deleting a directory is the same as deleting a file.