2 回答
TA贡献1772条经验 获得超5个赞
您可以使用ioutil.ReadAll:
bytes, err := ioutil.ReadAll(r)
// err management here
s := string(bytes)
TA贡献1780条经验 获得超1个赞
不确定这是否是您想要的:
package main
import (
"fmt"
"log"
"strings"
)
func main() {
s := "Hello world"
r := strings.NewReader(s)
buf := make([]byte, 100)
i, err := r.Read(buf)
if err != nil {
log.Fatal(err)
}
fmt.Println("length string:", len(s))
fmt.Println("bytes read:", i)
fmt.Println(buf)
}
输出:
length string: 11
bytes read: 11
[72 101 108 108 111 32 119 111 114 108 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
- 2 回答
- 0 关注
- 191 浏览
添加回答
举报