Skip to content

Instantly share code, notes, and snippets.

@padurean
Forked from crgimenes/stringToReaderCloser.go
Created September 27, 2022 11:34
Show Gist options
  • Save padurean/12bd4c3e85107c84be7626655ee1ebd0 to your computer and use it in GitHub Desktop.
Save padurean/12bd4c3e85107c84be7626655ee1ebd0 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