其他分享
首页 > 其他分享> > go的stream

go的stream

作者:互联网

go的stream

// MyBuffer project main.go
package main
 
import (
    "bytes"
    "fmt"
    "os"
)
 
func main() {
    buf1 := bytes.NewBuffer([]byte{'H', 'E', 'L', 'L', 'O'})
    buf2 := bytes.NewBuffer([]byte("hello"))
    buf3 := bytes.NewBufferString("world!")
 
    fmt.Println(buf1.String())
    fmt.Println(buf2.String())
    fmt.Println(buf3.String())
 
    s := []byte("WORLD!")
    n, err := buf1.Write(s)
    if err == nil {
        fmt.Println("n = ", n)
        fmt.Println("buf1 = ", buf1.String())
    } else {
        fmt.Println(err.Error())
    }
 
    n, err = buf3.WriteString(" plleh")
    if err == nil {
        fmt.Println("n = ", n)
        fmt.Println("buf3 = ", buf3.String())
    }
 
    var t byte = '?'
    err = buf2.WriteByte(t)
    if err == nil {
        fmt.Println("buf2 = ", buf2.String())
    } else {
        fmt.Println("err = ", err.Error())
    }
 
    r := []rune("中国人")
    n, err = buf2.WriteRune(r[0])
    if err == nil {
        fmt.Println("buf2 = ", buf2.String())
        fmt.Println("buf2.len = ", buf2.Len())
    } else {
        fmt.Println("err = ", err.Error())
    }
 
    file, _ := os.Create("out.txt")
    defer file.Close()
 
    buf2.WriteTo(file)
 
    buf1.WriteTo(os.Stdout)
    buf3.WriteTo(os.Stderr)
}

 

标签:stream,err,buf1,fmt,buf2,Println,go,String
来源: https://www.cnblogs.com/hnxxcxg/p/16085110.html