Skip to content

Instantly share code, notes, and snippets.

@xkou
Forked from crgimenes/stringToReaderCloser.go
Created June 9, 2022 02:20
Show Gist options
  • Save xkou/fe715ddf35e0758419db13910e79eace to your computer and use it in GitHub Desktop.
Save xkou/fe715ddf35e0758419db13910e79eace to your computer and use it in GitHub Desktop.
string to io.ReadCloser
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
func main() {
r := io.NopCloser(strings.NewReader("Hello, world!")) // r type is io.ReadCloser
// example to test r
buf := new(bytes.Buffer)
n, err := buf.ReadFrom(r)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
r.Close()
s := buf.String()
fmt.Printf("%d bytes read, %q\r\n", n, s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment