Standard Library Utilities
Bytes Buffer
A bytes.Buffer builds text step by step when a program needs to assemble a result.
buffer
A buffer holds pieces of data while a program builds a final value.
Bytes Buffer
bytes_buffer.go
Replay: real traced execution (multi-file project)
package main
import (
"bytes"
"fmt"
)
func main() {
var word = "alpha"
var buffer bytes.Buffer
buffer.WriteString("item:")
buffer.WriteString(word)
buffer.WriteString(":done")
result := buffer.String()
fmt.Println("word=", word)
fmt.Println("result=", result)
fmt.Println("length=", len(result))
}
package main
import (
"bytes"
"fmt"
)
func main() {
var word = "beta"
var buffer bytes.Buffer
buffer.WriteString("item:")
buffer.WriteString(word)
buffer.WriteString(":done")
result := buffer.String()
fmt.Println("word=", word)
fmt.Println("result=", result)
fmt.Println("length=", len(result))
}
package main
import (
"bytes"
"fmt"
)
func main() {
var word = "go"
var buffer bytes.Buffer
buffer.WriteString("item:")
buffer.WriteString(word)
buffer.WriteString(":done")
result := buffer.String()
fmt.Println("word=", word)
fmt.Println("result=", result)
fmt.Println("length=", len(result))
}
word ← "alpha", buffer ← bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0}
8func main() {9 var word→ "alpha" = "alpha" //@word="beta", "go"10 var buffer→ bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0} bytes.Buffer11 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a}, off:0, lastRead:0}.WriteString("item:")12 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x61, 0x6c, 0x70, 0x68, 0x61}, off:0, lastRead:0}.WriteString(word"alpha")13 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.WriteString(":done")14 result→ "item:alpha:done" := bufferbytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.String()1516 fmt.Println("word=", word"alpha")17 fmt.Println("result=", result"item:alpha:done")18 fmt.Println("length=", len(result"item:alpha:done"))19}outputword= alpha result= item:alpha:done length= 15
word ← "beta", buffer ← bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0}
8func main() {9 var word→ "beta" = "beta"10 var buffer→ bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0} bytes.Buffer11 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a}, off:0, lastRead:0}.WriteString("item:")12 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x62, 0x65, 0x74, 0x61}, off:0, lastRead:0}.WriteString(word"beta")13 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x62, 0x65, 0x74, 0x61, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.WriteString(":done")14 result→ "item:beta:done" := bufferbytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x62, 0x65, 0x74, 0x61, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.String()1516 fmt.Println("word=", word"beta")17 fmt.Println("result=", result"item:beta:done")18 fmt.Println("length=", len(result"item:beta:done"))19}outputword= beta result= item:beta:done length= 14
word ← "go", buffer ← bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0}
8func main() {9 var word→ "go" = "go"10 var buffer→ bytes.Buffer{buf:[]uint8(nil), off:0, lastRead:0} bytes.Buffer11 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a}, off:0, lastRead:0}.WriteString("item:")12 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x67, 0x6f}, off:0, lastRead:0}.WriteString(word"go")13 buffer→ bytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x67, 0x6f, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.WriteString(":done")14 result→ "item:go:done" := bufferbytes.Buffer{buf:[]uint8{0x69, 0x74, 0x65, 0x6d, 0x3a, 0x67, 0x6f, 0x3a, 0x64, 0x6f, 0x6e, 0x65}, off:0, lastRead:0}.String()1516 fmt.Println("word=", word"go")17 fmt.Println("result=", result"item:go:done")18 fmt.Println("length=", len(result"item:go:done"))19}outputword= go result= item:go:done length= 12