Go language basic seek cursor position os package

grammar:

os.Open()-->*File,err  *****

File.Seek(offset, whence), set the unknown cursor

offset, offset

whence, where to start: 0 at the beginning, 1 at the beginning, 2 at the end

package main

import (
   "os"
   "fmt"
)

func main () {
    /*
    seek(offset int, whence int), which means to set the cursor position
       offset int, set how many bytes
    whence int, where to start
       0,
       1 from the beginning of the file, 2 from the current position
       , 2 from the end of the file
     */
     //file,_:= os.Open("C:\\liu\\pro\\aa.txt") //RDONLY
     file , _:=os.OpenFile( "C:\\liu\\pro \\aa.txt" , os. O_RDWR , 0 )
     //1. After opening the file, the cursor defaults to the beginning of the file.
    bs := make ([] byte , 1 )
     defer file.Close()

    //2.seek()
     //Set the cursor position at: 4 bytes from the beginning of the file.
    //file.Seek(8,0)
     count , _:= file.Read(bs)
   fmt.Println(string(bs[:count])) //a
file.Seek(4,2)
   //count, _= file.Read(bs)
   //fmt.Println(string(bs[:count])) //
file.Write([]byte{65,66,67})
       
   fmt.Println( "Finished..." )
}

Guess you like

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