Golang 利用字节缓冲区 bytes.Buffer 创建字符串

示例代码:

package main

import (
	"bytes"
	"fmt"
	"reflect"
)

func main() {
	var buffer bytes.Buffer

	buffer.WriteString("www.ydook.com")
	buffer.WriteString("\n")
	buffer.WriteString("welcome!")
	buffer.WriteString("ITers happy amusement park!")

	fmt.Println("buffer = \n", buffer)
	fmt.Println("buffer type = \n", reflect.TypeOf(buffer))

	fmt.Println("the string of buffer = \n", buffer.String())
	fmt.Println("the type of the string of buffer = \n", reflect.TypeOf(buffer.String()))

}

运行结果:

buffer = 
 {[119 119 119 46 121 100 111 111 107 46 99 111 109 10 119 101 108 99 111 109 101 33 73 84 101 114 115 32 104 97 112 112 121 32 97 109 117 115 101 109 101 110 116 32 112 97 114 107 33] 0 0}
buffer type = 
 bytes.Buffer
the string of buffer = 
 www.ydook.com
welcome!ITers happy amusement park!
the type of the string of buffer = 
 string



你可能感兴趣的:(Golang)