In the strings module, there is a function func NewReader(s string) *Reader to create a Reader
from a string.
How can you get/read the string from the strings.Reader
?
You can use ioutil.ReadAll :
bytes, err := ioutil.ReadAll(r)
// err management here
s := string(bytes)
Not sure if this is what you want:
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)
}
output:
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]