This is a creation in Article, where the information may have evolved or changed.
Golang (go language) standard library analysis OS (7)
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
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}
(2) (f File). Read () This is a pointer to the function, which belongs to the File method, function prototype func (f *file) read (b []byte) (n int, err error) input the number of bytes read, return the length of bytes and error information
import ( "FMT" "OS") Func main () { b := make ([]byte, 100) //sets 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 /* n, e := f.read of this realization (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}
(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
Import ("FMT" "OS") Func main () {f, _: = OS. Open ("11.go") B: = make ([]byte,] n, _: = F.readat (b) fmt. PRINTLN (n) fmt. Println (String (b[:n))}
(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
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 number of reads <=0 is read all files returned []fileinfo for I, fi: = R Ange ff {FMT. Printf ("FileName%d:%+v\n", I, fi.) Name ())///Our output file names}}
(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
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)}
}
(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
Import ("FMT" "OS") Func main () {b: = make ([]byte, Ten) F, _: = OS. Open ("1.go") defer F.close () F.seek (1, 0)//equals start position offset 1 N, _: = F.read (b) fmt. Println (String (b[:n))//original character package output ackage}
(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
import ( "FMT" "OS") Func main () { f, _ := os. OpenFile ("1.go", os. O_rdwr|os. o_append, 0755) //to open the file n, _ := f.write ([]byte (") in Append and read-write mode. Helloword ")) //we write hellword fmt. PRINTLN (n) //the number of bytes written by b := make ([] BYTE,&NBSP;20) f.seek (0, 0) // The pointer returns to 0 data, _ := f.read (b) fmt. Println (String (b[:d ata)) //output of Packagehelloword}
(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
Import ("FMT" "OS") Func main () {f, _: = OS. OpenFile ("1.go", OS. O_RDWR, OS. Modeperm) F.writeat ([]byte ("Widuu"), 10)//write at offset 10 where B: = make ([]byte] D, _: = F.readat (b, 10)//offset 10 where open Start reading the FMT. Println (String (b[:d]))//widuudhellowordhello}
(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.
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 always be at the end of the write C, _: = F.read (b) fmt. Println (String (B[:c]))//return Hello Word Widuu}
All right, let's finish this today and we'll go ahead and talk about OS packs tomorrow.
This article starts out in the Micro network http://www.widuu.com/archives/01/922.html also can be viewed via github with the source code in the GitHub address!
0 recommended 0 Favorites