最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Go語(yǔ)言編程中對(duì)文件讀寫(xiě)的基本方法整理

 更新時(shí)間:2015年10月27日 15:30:05   投稿:goldensun  
這篇文章主要介紹了Go語(yǔ)言編程中對(duì)文件讀寫(xiě)的基本方法整理,是Go語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

1.func Copy(dst Writer, src Reader) (written int64, err error)這個(gè)函數(shù)是從一個(gè)文件讀取拷貝到另外一個(gè)文件,一直拷貝到讀取文件的EOF,所以不會(huì)返回io.EOF錯(cuò)誤,參數(shù)是寫(xiě)入目標(biāo)器和讀取目標(biāo)器,返回int64的拷貝字節(jié)數(shù)和err信息

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 r, _ := os.Open("test.txt")
 w, _ := os.Create("write.txt")
 num, err := io.Copy(w, w)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(num) //返回int64的11 打開(kāi)我的write.txt正是test.txt里邊的hello widuu


2.func CopyN(dst Writer, src Reader, n int64) (written int64, err error)看函數(shù)就知道了跟上述的是一樣的,只是多加了一個(gè)讀取數(shù)的限制,然后我們看下代碼

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "io/ioutil"
 "os"
)

func main() {
 r, _ := os.Open("test.txt")
 w, _ := os.Create("write1.txt")
 num, err := io.CopyN(w, r, 5)
 if err != nil {
  fmt.Println(err)
 }
 defer r.Close()
 b, _ := ioutil.ReadFile("write1.txt")
 fmt.Println(string(b)) //輸出 hello
 fmt.Println(num)       //5
}


3.func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)這個(gè)函數(shù)就是從讀取器中讀取數(shù)據(jù)放到我們的buf中,限定了最小的讀取字節(jié)數(shù),如果我們讀取的數(shù)據(jù)小于最小讀取器,譬如你設(shè)定min的值是8,但是你讀取的數(shù)據(jù)字節(jié)數(shù)是5就會(huì)返回一個(gè)`io.ErrUnexpectedEOF`,如果大于就會(huì)返回`io.ErrShortBuffer`,讀取完畢會(huì)有`io.EOF`~~,多講一些哈,這個(gè)Reader只要我們滿足這個(gè)interface就可以用這個(gè)

復(fù)制代碼 代碼如下:

type Reader interface {
    Read(p []byte) (n int, err error)
}


其中*File就支持func (f *File) Read(b []byte) (n int, err error)

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 r, _ := os.Open("write1.txt")
 b := make([]byte, 20)
 defer r.Close()
 var total int
 for {
  n, err := io.ReadAtLeast(r, b, 8)
  if err == nil {
   fmt.Println("Read enough value:", string(b)) // Read enough value: hello widuu
  }
  if err == io.ErrUnexpectedEOF { //讀取了的數(shù)據(jù)小于我們限定的最小讀取數(shù)據(jù)8
   fmt.Println("Read fewer value:", string(b[0:n]))
  }
  
  if err == io.ErrShortBuffer{   //這個(gè)是我們?cè)O(shè)定的buf也就是b小于我們限定8
   fmt.Println("buf too Short")
   os.Exit(1)
  }
  if err == io.EOF { //讀完了 輸出
   fmt.Println("Read end total", total) //Read end total 11
   break
  }
  total = total + n
 }
}


4.func ReadFull(r Reader, buf []byte) (n int, err error)這個(gè)函數(shù)和上邊的函數(shù)是相似,只不過(guò)是讀取len(buf)個(gè),放在buf中

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 r, _ := os.Open("write.txt")
 b := make([]byte, 20)
 num, err := io.ReadFull(r, b)
 defer r.Close()
 if err == io.EOF {
  fmt.Println("Read end total", num)
 }
 if err == io.ErrUnexpectedEOF {
  fmt.Println("Read fewer value:", string(b[:num])) //Read fewer value: hello widuu,依然是buf長(zhǎng)度大于讀取的長(zhǎng)度
  return
 }

 fmt.Println("Read  value:", string(b)) //如果b是5 就出現(xiàn)這里
}


5.func WriteString(w Writer, s string) (n int, err error)弄完讀了,當(dāng)然帶要寫(xiě)了,這個(gè)函數(shù)主要是向?qū)懭肽繕?biāo)中寫(xiě)入字符創(chuàng),返回是寫(xiě)入的字節(jié)數(shù)還有error錯(cuò)誤,主要是權(quán)限的錯(cuò)誤,其中寫(xiě)入呀!都是writer這個(gè)結(jié)構(gòu)就可以寫(xiě)入

復(fù)制代碼 代碼如下:

type Writer interface {
    Write(p []byte) (n int, err error)
}
跟read一樣我們的*File是有func (f *File) Write(b []byte) (n int, err error),當(dāng)然其實(shí)我們的*File中就已經(jīng)有WirteString了func (f *File) WriteString(s string) (ret int, err error)
import (
 "fmt"
 "io"
 "io/ioutil"
 "os"
)

func main() {
 w, _ := os.OpenFile("write1.txt", os.O_RDWR, os.ModePerm)
 n, err := io.WriteString(w, "ni hao ma")
 if err != nil {
  fmt.Println(err) //當(dāng)我用os.open()的時(shí)候木有權(quán)限  悲催的~~輸出write write1.txt: Access is denied.
 }
 defer w.Close()
 b, _ := ioutil.ReadFile("write1.txt")
 fmt.Println("write total", n) //write total 9
 fmt.Println(string(b))        // ni hao ma
}


6.type LimitedReader

復(fù)制代碼 代碼如下:

type LimitedReader struct {
    R Reader // 讀取器了
    N int64  // 最大字節(jié)限制
}


只實(shí)現(xiàn)了一個(gè)方法func (l *LimitedReader) Read(p []byte) (n int, err error)其實(shí)我們不難發(fā)現(xiàn)這個(gè)跟我們的ReadAtLast()就是親兄弟的節(jié)奏

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "os"
)

func main() {
 reader, _ := os.Open("test.txt")
 limitedreader := io.LimitedReader{
  R: reader,
  N: 20,
 }
 p := make([]byte, 10)
 var total int
 for {
  n, err := limitedreader.Read(p)
  if err == io.EOF {
   fmt.Println("read total", total)     //read total 11
   fmt.Println("read value", string(p)) //read value hello widuu
   break
  }
  total = total + n

 }

}


7.type PipeReader

復(fù)制代碼 代碼如下:

type PipeReader struct {
    // contains filtered or unexported fields
}


(1)func Pipe() (*PipeReader, *PipeWriter)創(chuàng)建一個(gè)管道,并返回它的讀取器和寫(xiě)入器,這個(gè)會(huì)在內(nèi)存中進(jìn)行管道同步,它的開(kāi)啟會(huì)io.Reader然后等待io.Writer的輸入,沒(méi)有內(nèi)部緩沖,它是安全的調(diào)用Read和Write彼此和并行調(diào)用寫(xiě)

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
 "reflect"
)

func main() {
 r, w := io.Pipe()
 fmt.Println(reflect.TypeOf(r)) //*io.PipeReader
 fmt.Println(reflect.TypeOf(w)) //*io.PipeWriter
}


(2)func (r *PipeReader) Close() error管道關(guān)閉后,正在進(jìn)行或后續(xù)的寫(xiě)入Write操作返回ErrClosedPipe
復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 r.Close()

 _, err := w.Write([]byte("hello widuu")) 

 if err == io.ErrClosedPipe {
  fmt.Println("管道已經(jīng)關(guān)閉無(wú)法寫(xiě)入") //管道已經(jīng)關(guān)閉無(wú)法寫(xiě)入
 }
}


(3)func (r *PipeReader) CloseWithError(err error) error這個(gè)就是上邊的r.Close關(guān)閉的時(shí)候,寫(xiě)入器會(huì)返回錯(cuò)誤的信息

復(fù)制代碼 代碼如下:

import (
 "errors"
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 r.Close()
 err := errors.New("管道符關(guān)閉了") //errors這個(gè)包我們前邊已經(jīng)說(shuō)過(guò)了,就一個(gè)方法New不會(huì)的可以看看前邊的
 r.CloseWithError(err)
 _, err = w.Write([]byte("test"))
 if err != nil {
  fmt.Println(err) //管道符關(guān)閉了
 }
}


(4)func (r *PipeReader) Read(data []byte) (n int, err error)標(biāo)準(zhǔn)的閱讀接口,它從管道中讀取數(shù)據(jù)、阻塞一直到一個(gè)寫(xiě)入接口關(guān)閉,如果寫(xiě)入端發(fā)生錯(cuò)誤,它就會(huì)返回錯(cuò)誤,否則返回的EOF

復(fù)制代碼 代碼如下:

import (
 "fmt"
 "io"
)

func main() {
 r, w := io.Pipe()
 go w.Write([]byte("hello widuu"))
 d := make([]byte, 11)
 n, _ := r.Read(d) //從管道里讀取數(shù)據(jù)
 fmt.Println(string(d))
 fmt.Println(n)
}


相關(guān)文章

  • Go設(shè)計(jì)模式之訪問(wèn)者模式講解和代碼示例

    Go設(shè)計(jì)模式之訪問(wèn)者模式講解和代碼示例

    訪問(wèn)者是一種行為設(shè)計(jì)模式, 允許你在不修改已有代碼的情況下向已有類(lèi)層次結(jié)構(gòu)中增加新的行為,本文將通過(guò)代碼示例給大家詳細(xì)的介紹一下Go設(shè)計(jì)模式之訪問(wèn)者模式,需要的朋友可以參考下
    2023-08-08
  • goland 實(shí)現(xiàn)自動(dòng)格式化代碼

    goland 實(shí)現(xiàn)自動(dòng)格式化代碼

    這篇文章主要介紹了goland 實(shí)現(xiàn)自動(dòng)格式化代碼的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • Go語(yǔ)言中如何確保Cookie數(shù)據(jù)的安全傳輸

    Go語(yǔ)言中如何確保Cookie數(shù)據(jù)的安全傳輸

    這篇文章主要介紹了Go語(yǔ)言中如何確保Cookie數(shù)據(jù)的安全傳輸,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 如何在VScode 中編譯多個(gè)Go文件

    如何在VScode 中編譯多個(gè)Go文件

    這篇文章主要介紹了VScode 中編譯多個(gè)Go文件的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Go實(shí)現(xiàn)mongodb增刪改查工具類(lèi)的代碼示例

    Go實(shí)現(xiàn)mongodb增刪改查工具類(lèi)的代碼示例

    這篇文章主要給大家介紹了關(guān)于Go實(shí)現(xiàn)mongodb增刪改查工具類(lèi)的相關(guān)資料,MongoDB是一個(gè)NoSQL數(shù)據(jù)庫(kù),它提供了靈活的文檔存儲(chǔ)模型以及強(qiáng)大的查詢(xún)和操作功能,需要的朋友可以參考下
    2023-10-10
  • golang獲取prometheus數(shù)據(jù)(prometheus/client_golang包)

    golang獲取prometheus數(shù)據(jù)(prometheus/client_golang包)

    本文主要介紹了使用Go語(yǔ)言的prometheus/client_golang包來(lái)獲取Prometheus監(jiān)控?cái)?shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • Golang logrus 日志包及日志切割的實(shí)現(xiàn)

    Golang logrus 日志包及日志切割的實(shí)現(xiàn)

    這篇文章主要介紹了Golang logrus 日志包及日志切割的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 一文帶你讀懂Golang?sync包之sync.Mutex

    一文帶你讀懂Golang?sync包之sync.Mutex

    sync.Mutex可以說(shuō)是sync包的核心了,?sync.RWMutex,?sync.WaitGroup...都依賴(lài)于他,?本章我們將帶你一文讀懂sync.Mutex,快跟隨小編一起學(xué)習(xí)一下吧
    2023-04-04
  • Go語(yǔ)言與gRPC的完美結(jié)合實(shí)戰(zhàn)演練

    Go語(yǔ)言與gRPC的完美結(jié)合實(shí)戰(zhàn)演練

    這篇文章主要介紹了Go語(yǔ)言與gRPC的完美結(jié)合實(shí)戰(zhàn)演練,gRPC(Remote?Procedure?Call)是一種遠(yuǎn)程過(guò)程調(diào)用技術(shù),通過(guò)壓縮和序列化數(shù)據(jù)來(lái)優(yōu)化網(wǎng)絡(luò)通信,可以顯著提高服務(wù)調(diào)用的性能和效率
    2024-01-01
  • Hugo?Config模塊構(gòu)建實(shí)現(xiàn)源碼剖析

    Hugo?Config模塊構(gòu)建實(shí)現(xiàn)源碼剖析

    這篇文章主要為大家介紹了Hugo?Config模塊構(gòu)建實(shí)現(xiàn)源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論

泾阳县| 霍邱县| 邮箱| 龙海市| 甘孜县| 秦皇岛市| 鸡泽县| 新绛县| 大足县| 旅游| 印江| 玉溪市| 江永县| 孙吴县| 庆云县| 全州县| 获嘉县| 榆树市| 黄陵县| 台中县| 兴隆县| 财经| 揭东县| 浮梁县| 庆云县| 剑川县| 桐乡市| 古交市| 黄石市| 景宁| 华蓥市| 余江县| 秦安县| 磴口县| 宝清县| 新丰县| 秭归县| 黔江区| 兰州市| 大新县| 德格县|