This is a creation in Article, where the information may have evolved or changed.
Golang Standard Library
Today we continue to talk about Golang standard library OS package, still no nonsense directly to
(1) (f *file). Name () This function is the name of the returned file, function prototype func (f *file) name () string to file pointer operation, return string, feel compare chicken help method underlying implementation
[PHP]
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
}
[/php]
(2) (f *file). Read () This is the function of the pointer to operate, belongs to the *file method, function prototype func (f *file) read (b []byte) (n int, err error) input bytes read, return the length of bytes and error information
[PHP]
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))//output content why is n not directly input 100? The bottom-level 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 the byte is less than 100 to read n
}
[/php]
(3) (f *file). ReadAt () The prototype of this function is the Func (F *file) ReadAt (b []byte, off Int64) (n int, err error) with subscript, which can be customized to read how much
[PHP]
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]))
}
[/php]
(4) (f *file). Readdir () function prototype func (f *file) Readdir (n int) (FI []fileinfo, err Error), we want to open a folder, then set the number of Read folder files, return the file FileInfo information
[PHP]
Import (
"FMT"
"OS"
)
Func Main () {
F, err: = OS. Open ("src")//Opens a directory
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Defer F.close ()
FF, _: = F.readdir (10)//Set the number of reads <=0 is to 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
}
}
[/php]
(5) (f *file). Readdirnames This function is to read the file name within the directory, in fact, the previous function we have implemented the function of functions, the function of the prototype func (F *file) readdirnames (n int) (names []string, err Error ), with the top only return is the filename []string's Slice
[PHP]
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)
}
}
[/php]
(6) (f *file). Seek () This function everyone understand, is the address of the offset pointer, the function of the prototype is Func (f *file) Seek (offset int64, whence int) (ret int64, err error) where offset is the position of the file pointer wh ence is 0 o'clock for the position relative to the file, 1 for the relative current position, and 2 for the position relative to the end of the file. RET returns the position of the current pointer
[PHP]
Import (
"FMT"
"OS"
)
Func Main () {
B: = make ([]byte, 10)
F, _: = OS. Open ("1.go")
Defer F.close ()
F.seek (1, 0)//equivalent to start position offset 1
N, _: = F.read (b)
Fmt. Println (String (b[:n))//original character package output ackage
}
[/php]
(7) (f *file). Stat () which followed the front of the OS. Stat () is the same as returning FileInfo so don't talk much.
(8) (f *file). Truncate () This function with the front of the OS. The Truncate () function is the same. I'm not going to tell you more. OS (5)
(9) (f *file) Write file, function prototype func (f *file) write (b []byte) (n int, err error) returns the number of bytes written by n
[PHP]
Import (
"FMT"
"OS"
)
Func Main () {
F, _: = OS. OpenFile ("1.go", OS. O_rdwr|os. O_append, 0755)//Open file in Append and read-write mode
N, _: = F.write ([]byte ("Helloword"))//We write Hellword
Fmt. PRINTLN (n)//number of bytes written to print
B: = make ([]byte, 20)
F.seek (0, 0)//pointer back to 0
Data, _: = F.read (b)
Fmt. Println (String (b[:d ata))//Output Packagehelloword
}
[/php]
(10) (f *file) Writeat () writes in 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
[PHP]
Import (
"FMT"
"OS"
)
Func Main () {
F, _: = OS. OpenFile ("1.go", OS. O_RDWR, OS. Modeperm)
F.writeat ([]byte ("Widuu"), 10)//write in place of offset 10
B: = make ([]byte, 20)
D, _: = F.readat (b, 10)//offset 10 start reading
Fmt. Println (String (b[:d))//widuudhellowordhello
}
[/php]
(11) (f *file). WriteString () This is very simple, write the String function prototype func (f *file) WriteString (S string) (ret int, err error) returns the same value.
[PHP]
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)//Make sure to return the offset address to 0 otherwise it will always be at the end of the write
C, _: = F.read (b)
Fmt. Println (String (B[:c]))//return Hello Word widuu
}
[/php]
All right, let's finish this today, and we'll talk about OS packs tomorrow.
Without permission, do not reprint this site any article: Micro network»golang (go Language) standard library analysis of the OS (7)