1. and
func main() { var byteArr []byte buf := (byteArr) ([]byte("Good today")) (()) }
package main import ( "bytes" "fmt" ) func main() { data := []byte("Lu Duosin's thoughts") reader := (data) // Read the entire byte array buf := make([]byte, len(data)) _, err := (buf) if err != nil { ("Read failed:", err) } ("Bytes read:", buf) // Read part of the byte slice part := make([]byte, 3) _, err = (part) if err != nil { ("Read failed:", err) } ("Bytes read:", part) // Find and read a character in a byte slice offset, err := (6, 0) if err != nil { ("Seek failed:", err) } ch, size, err := () if err != nil { ("ReadRune failed:", err) } ("Read %c with size %d at offset %d\n", ch, size, offset) }
2. (No newWriter)
r := ("abcdefghijklmn") (()) // Output 14 Initially, the unread length is equal to the string length var buf []byte buf = make([]byte, 5) readLen, err := (buf) ("Length read:", readLen) //The length read is 5 if err != nil { ("mistake:", err) } (buf) //adcde (()) //9 5 reads, the remaining unread is 14-5 (()) //14 length of string
III. and
package main import ( "fmt" "io" "os" ) func main() { ("Hike.com()") fileName := "C:/" file, err := (fileName) if err != nil{ ("Read file err, err =", err) return } defer () var chunk []byte buf := make([]byte, 1024) for{ n, err := (buf) if err != nil && err != { ("read buf fail", err) return } //Instructions the end of reading if n == 0 { break } //Read to the final buffer chunk = append(chunk, buf[:n]...) } ("File Content =", string(chunk)) }
package main import ( "bufio" "fmt" "io/ioutil" "os" ) func main() { ("Hike.com()") var( fileName = "C:/" content = "Hello HaiCoder" file * err error ) //Use append mode to open the file file, err = (fileName, os.O_APPEND, 0666) if err != nil{ ("Open file err =", err) return } defer () writer := (file) //Write to file n, err := ([]byte(content)) if err != nil{ ("Write file err =", err) return } ("Write file success, n =", n) () //Read the file fileContent, err := (fileName) if err != nil{ ("Read file err =", err) return } ("Read file success =", string(fileContent)) }
Summarize:
1. The newbuffer used is used to store contents, and the specific memory size required cannot be determined. Therefore, the parameters of the newbuffer are not fixed []byte{};
2. The cache area used for the newreader should have numerical values, and it is used to read the contents in it at this time.
This is the end of this article about the use of various newreaders and newbuffers in Go. For more related Go reader buffer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!