Skip to content

Instantly share code, notes, and snippets.

@amelhusic
Forked from crgimenes/stringToReaderCloser.go
Created November 19, 2020 13:24
Show Gist options
  • Select an option

  • Save amelhusic/56fca85dce24a1fdc48d0a329295c058 to your computer and use it in GitHub Desktop.

Select an option

Save amelhusic/56fca85dce24a1fdc48d0a329295c058 to your computer and use it in GitHub Desktop.
string to io.ReadCloser
package main
import (
"bytes"
"fmt"
"io/ioutil"
)
func main() {
r := ioutil.NopCloser(bytes.NewReader([]byte("hello world"))) // r type is io.ReadCloser
// example to test r
buf := new(bytes.Buffer)
buf.ReadFrom(r)
r.Close()
s := buf.String()
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment