Go語言文件操作的方法
更新時間:2015年02月20日 15:53:53 作者:不吃皮蛋
這篇文章主要介紹了Go語言文件操作的方法,涉及文件的讀寫及關(guān)閉等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Go語言文件操作的方法。分享給大家供大家參考。具體如下:
關(guān)閉文件:
復(fù)制代碼 代碼如下:
func (file *File) Close() os.Error {
if file == nil {
return os.EINVAL
}
e := syscall.Close(file.fd)
file.fd = -1 // so it can't be closed again
if e != 0 {
return os.Errno(e)
}
return nil
}
if file == nil {
return os.EINVAL
}
e := syscall.Close(file.fd)
file.fd = -1 // so it can't be closed again
if e != 0 {
return os.Errno(e)
}
return nil
}
文件讀?。?br />
復(fù)制代碼 代碼如下:
func (file *File) Read(b []byte) (ret int, err os.Error) {
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Read(file.fd, b)
if e != 0 {
err = os.Errno(e)
}
return int(r), err
}
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Read(file.fd, b)
if e != 0 {
err = os.Errno(e)
}
return int(r), err
}
寫文件:
復(fù)制代碼 代碼如下:
func (file *File) Write(b []byte) (ret int, err os.Error) {
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Write(file.fd, b)
if e != 0 {
err = os.Errno(e)
}
return int(r), err
}
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Write(file.fd, b)
if e != 0 {
err = os.Errno(e)
}
return int(r), err
}
獲取文件名:
復(fù)制代碼 代碼如下:
func (file *File) String() string {
return file.name
}
return file.name
}
希望本文所述對大家的Go語言程序設(shè)計有所幫助。
相關(guān)文章
GO語言中創(chuàng)建切片的三種實現(xiàn)方式
這篇文章主要介紹了GO語言中創(chuàng)建切片的三種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Go語言并發(fā)之context標(biāo)準(zhǔn)庫的使用詳解
Context的出現(xiàn)是為了解決在大型應(yīng)用程序中的并發(fā)環(huán)境下,協(xié)調(diào)和管理多個goroutine之間的通信、超時和取消操作的問題,本文就來和大家簡單聊聊它的具體用法,希望對大家有所幫助2023-06-06
GoLang中生成UUID唯一標(biāo)識的實現(xiàn)方法
UUID是讓分散式系統(tǒng)中的所有元素,都能有唯一的辨識信息,本文主要介紹了GoLang中生成UUID唯一標(biāo)識的實現(xiàn)方法,具有一定的參考價值,感興趣的可以了解一下2024-08-08
Golang內(nèi)存泄露場景與定位方式的實現(xiàn)
Golang有自動垃圾回收機(jī)制,但是仍然可能會出現(xiàn)內(nèi)存泄漏的情況,本文主要介紹了Golang內(nèi)存泄露場景與定位方式的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-04-04

