本文主要是介绍go标准库io、ioutil,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
标准库io
io. WriteString
s输出到w
func WriteString(w Writer, s string) (n int, err error)
io.Copy
自动以32k字节的buf进行遍历复制直到遇到io.EOF。返回总共复制的字节数,如果成功err返回nil
func Copy(dst Writer, src Reader) (written int64, err error) {return copyBuffer(dst, src, nil) }
例如
func handlefunc(w http.ResponseWriter, r *http.Request) {f, _ := os.Open("./1.txt")defer f.Close()_,err := io.Copy(w,f)errCheck(err)
}
io.CopyBuffer
比io.Copy多一项可以自定buf大小
func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {if buf != nil && len(buf) == 0 {panic("empty buffer in io.CopyBuffer")}return copyBuffer(dst, src, buf) }
io.CopyN
只复制前n字节,src内容足够n个字节时err返回nil,否则返回io.EOF
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
io.MultiReader
func MultiReader(readers ...Reader) Reader
r1 := strings.NewReader("first reader ")r2 := strings.NewReader("second reader ")r3 := strings.NewReader("third reader\n")r := io.MultiReader(r1, r2, r3)if _, err := io.Copy(os.Stdout, r); err != nil {log.Fatal(err)}// Output: first reader second reader third reader
io.Multiwriter
func MultiWriter(writers ...Writer) Writer
r := strings.NewReader("some io.Reader stream to be read\n")var buf1, buf2 bytes.Bufferw := io.MultiWriter(&buf1, &buf2)if _, err := io.Copy(w, r); err != nil {log.Fatal(err)}fmt.Print(buf1.String())fmt.Print(buf2.String())// Output:// some io.Reader stream to be read// some io.Reader stream to be read
io.pipe
读和写用channel通信,读操作阻塞直到有被写入内容
func Pipe() (*PipeReader, *PipeWriter)
r, w := io.Pipe()go func() {fmt.Fprint(w, "some text to be read\n")w.Close()}()buf := new(bytes.Buffer)buf.ReadFrom(r)fmt.Print(buf.String())// Output: some text to be read
io.TeeReader
将w传给r的内容同样传给返回的reader
func TeeReader(r Reader, w Writer) Reader
io.NewSectionReader
从第off个字节开始读取n个字节
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader
r := strings.NewReader("some io.Reader stream to be read\n")s := io.NewSectionReader(r, 5, 16)buf := make([]byte, 6)if _, err := s.ReadAt(buf, 10); err != nil { //SectionReader的方法log.Fatal(err)}fmt.Printf("%s\n", buf)// Output: stream
r := strings.NewReader("some io.Reader stream to be read\n")if _, err := io.Copy(os.Stdout, r); err != nil {log.Fatal(err)}r.Seek(15, io.SeekStart)if _, err := io.Copy(os.Stdout, r); err != nil {log.Fatal(err)}r.Seek(-5, io.SeekEnd)if _, err := io.Copy(os.Stdout, r); err != nil {log.Fatal(err)}// Output:// some io.Reader stream to be read// stream to be read// read
其他: 至少读min个字节到buf,否则返回error: short buffer func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
将buf填满,否则返回error: unexpected EOF func ReadFull(r Reader, buf []byte) (n int, err error)
返回读取传入的reader前n个字节的reader func LimitReader(r Reader, n int64) Reader
标准库ioutil
ioutil.ReadAll
遍历增加函数内部buf容量并读取,直到把内容全部读到buf中(所以比其他方法消耗内存)
func ReadAll(r io.Reader) ([]byte, error)
ioutil.ReadFile
调用readall方法直接全部读取文件内容
func ReadFile(filename string) ([]byte, error)
ioutil.WriteFile
写内容到文件,如果文件不存在则新建,如果存在则清空再写
func WriteFile(filename string, data []byte, perm os.FileMode) error
ioutil.ReadDir
获取指定文件地址下面文件的信息
func ReadDir(dirname string) ([]os.FileInfo, error)
f,_:= ioutil.ReadDir("./")for index,fileInfo := range f{fmt.Println(index,fileInfo.Name())}
ioutil.TempFile
在指定地址生成一个以pattern为前缀随机数为后缀的随机名字的文件。每调用一次会更新内部的随机种子。如果过程中发现生成的文件名在该地址已存在,再进行一次生成,和已存在文件名不同时才新建文件。
func TempFile(dir, pattern string) (f *os.File, err error)
ioutil.TempDir
方法同上生成文件夹,返回生成的名字。
func TempDir(dir, prefix string) (name string, err error)
//文件夹操作content := []byte("temporary file's content")dir, err := ioutil.TempDir("", "example")if err != nil {log.Fatal(err)}defer os.RemoveAll(dir) // 文件夹不用可以删除tmpfn := filepath.Join(dir, "tmpfile")if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {log.Fatal(err)}//文件操作content := []byte("temporary file's content")tmpfile, err := ioutil.TempFile("", "example.*.txt")if err != nil {log.Fatal(err)}defer os.Remove(tmpfile.Name()) // 文件不用可以删除if _, err := tmpfile.Write(content); err != nil {tmpfile.Close()log.Fatal(err)}if err := tmpfile.Close(); err != nil {log.Fatal(err)}
这篇关于go标准库io、ioutil的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!