Go language base writes out data Writer() os package

The first step is to open a file first, create one if the file does not exist, and have write permission

OpenFile(filename,flag,perm)-->*File,err  *****

filename: file name 
flag, get the open mode of the file:
O_RDONLY read only
O_WRONLY write only
O_RDWR read and write

O_CREATE to create a file

perm, if the file does not exist, you need to specify the permission
0777 (readable and writable) of the created file

-rw-

The second step is to write data

                Write([]byte)-->count,err, write out the byte array

WriteAt([]byte,off), write data at the specified location.

WriteString(string), directly write out the string

package main

import (
   "os"
   "fmt"
)

func main () {
    /*
    Writer(), write data out
     */
     file , _:=os.OpenFile( "C:\\liu\\pro\\ao.txt" , os. O_WRONLY |os. O_CREATE , 0777 )
     defer file.Close()
     //Write the byte array directly,
 bs := [] byte { 97 , 98 , 99 , 100 , 101 , 102 } // af
 file.Write(bs[ 0 : 4 ]) //0123
 file.WriteString( "Facing the sea" )
   


           //abcd faces the sea
    /*
    16 bytes
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    abcd faces the sea
    abcd faces the big ABCDE
     */
 file.WriteAt([] byte { 'A' , 'B' , 'C' , 'D' , 'E' } , 13 )

   
    fmt.Println( "I'm done.." )
 }





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893909&siteId=291194637