For example, the common function usage of OS library in go language _golang

Source: Internet
Author: User

(f *file). Name () This function is to return the name of the file, function prototype func (f *file) name () string to the pointer operation of the file, return the string, feel the way to compare the chicken to help the bottom of the implementation

Copy Code code as follows:

Func (f *file) Name () string {return f.name}
Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Open ("1.go")
Fmt. Println (F.name ())//Output 1.go
}


(f *file). Read () This is the pointer to the function, which belongs to the *file method, the function prototype func (f *file) read (b []byte) (n int, err error), enter the number of bytes read, return the length of the byte and error information
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
B: = make ([]byte, 100)//Set the number of bytes read
F, _: = OS. Open ("11.go")
N, _: = F.read (b)
Fmt. PRINTLN (N)
Fmt. Println (String (b[:n]))//What is the output of n instead of entering 100 directly? The underlying implementation of this
/*
N, E: = F.read (b)
If n < 0 {
n = 0
}
if n = = 0 && len (b) > 0 && E = = Nil {
return 0, Io. Eof
}
*/
So bytes less than 100 reads n
}


(f *file). ReadAt () The prototype of this function is Func (f *file) ReadAt (b []byte, off Int64) (n int, err error) is added subscript, can be customized to read how much
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Open ("11.go")
B: = make ([]byte, 20)
N, _: = F.readat (b, 15)
Fmt. PRINTLN (N)
Fmt. Println (String (B[:n]))
}


(f *file). Readdir () function prototype func (f *file) Readdir (n int) (FI []fileinfo, err Error), we want to open a folder, and then set the number of Read folder files, return the file's FileInfo information
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, err: = OS. Open ("src")//Opening a directory
If Err!= nil {
Fmt. PRINTLN (ERR)
}
Defer F.close ()
FF, _: = F.readdir (10)//Set the number of reads <=0 is read all the files returned by []fileinfo
For I, FI: = Range ff {
Fmt. Printf ("FileName%d:%+v\n", I, fi.) Name ()///Our output file names
}
}


(f *file). Readdirnames This function is to read the file name in the directory, in fact, the last function we have implemented the function of the function, the prototype func (F *file) readdirnames (n int) (names []string, err Error ), with the top only returns the filename []string slice
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. Open ("Bin")
Names, err: = F.readdirnames (0)
If Err!= nil {
Fmt. PRINTLN (ERR)
}
For I, Name: = range Names {
Fmt. Printf ("FileName%d:%s\n", I, name)
}
}


(f *file). Seek () This function as you can see, is the address of the offset pointer, the prototype of the function is Func (f *file) Seek (offset int64, whence int) (ret int64, err error) where offset is the location of the file pointer wh ence 0 O'Clock represents the position relative to the beginning of the file, 1 represents the relative current position, and 2 represents the position relative to the end of the file. RET returns the position of the pointer now.
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
B: = make ([]byte, 10)
F, _: = OS. Open ("1.go")
Defer F.close ()
F.seek (1, 0)//equivalent to starting position offset 1
N, _: = F.read (b)
Fmt. Println (String (b[:n]))//original character package output ackage
}


(f *file) Write like file, function prototype func (f *file) write (b []byte) (n int, err error) returns the number of bytes written by n
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. OpenFile ("1.go", OS. O_rdwr|os. O_append, 0755)//To open the file in the form of append and read/write
N, _: = F.write ([]byte ("Helloword"))//We write to Hellword
Fmt. PRINTLN (n)//print bytes written
B: = make ([]byte, 20)
F.seek (0, 0)//pointer back to 0
Data, _: = F.read (b)
Fmt. Println (String (b[:d ata))//Output Packagehelloword
}


(f *file) Writeat () is written at the offset position, the function prototype is func (f *file) writeat (b []byte, off Int64) (n int, err error) The return value is the same
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. OpenFile ("1.go", OS. O_RDWR, OS. Modeperm)
F.writeat ([]byte ("Widuu"), 10)//write at offset 10
B: = make ([]byte, 20)
D, _: = F.readat (b, 10)//offset 10 to begin reading
Fmt. Println (String (b[:d]))//widuudhellowordhello
}


(f *file). WriteString () This is very simple, write String function prototype func (f *file) WriteString (S string) (ret int, err error) return the same value
Copy Code code as follows:

Import (
"FMT"
"OS"
)

Func Main () {
F, _: = OS. OpenFile ("2.go", OS. O_RDWR, OS. Modeperm)
N, _: = f.writestring ("Hello word widuu")//write String
Fmt. PRINTLN (N)
B: = make ([]byte, N)
F.seek (0, 0)//must return the offset address to 0 otherwise it will be at the end of the write
C, _: = F.read (b)
Fmt. Println (String (B[:c]))//back to Hello Word widuu
}

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.