Golang中seek使用方法詳解

來源:互聯網
上載者:User


習慣了php中的seek和tell,轉到golang時突然發現只有Seek發現,tell方法不見了。google了一下,發現了tell的實現方法:

File.Seek(0, os.SEEK_CUR) 或者File.Seek(0,1) 參考

解釋:

先來看下Seek方法

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

跳轉到文本中的某處,並返回此處的位移量

File.Seek(0, os.SEEK_CUR) #跳轉到當前位置(位置不變)

這樣就很好理解了。

f,_:=os.Open("a.txt")
//從頭開始,檔案指標位移100
f.Seek(100,0)
buffer:=make([]byte,1024)
// Read 後檔案指標也會位移
_,err:=f.Read(buffer)
if err!=nil{
    fmt.Println(nil)
    return
}
// 擷取檔案指標當前位置
cur_offset,_:=f.Seek(0,os.SEEK_CUR)
fmt.Printf('current offset is %d\n', cur_offset)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.