Golang file read/write

Source: Internet
Author: User
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.

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.