Go Package io: Difference between revisions
Jump to navigation
Jump to search
Line 28: | Line 28: | ||
bs := []byte("something") | bs := []byte("something") | ||
r := bytes.NewReader(bs) | r := bytes.NewReader(bs) | ||
</syntaxhighlight> | |||
==Getting a <tt>Reader</tt> from a <tt>string</tt>== | |||
<syntaxhighlight lang='go'> | |||
s := "some string" | |||
reader := strings.NewReader(s) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 17:56, 6 February 2024
External
Internal
Overview
The io package consists of a few functions, but mostly interfaces used by other packages.
Reader
An interface that exposes Read()
.
Reader
s can be used to unmarshall JSON into structs as shown here:
Files opened with os.Create()
implement io.Reader
.
Wrapping a []byte into a Reader
A []byte
can be wrapped into a Reader
with:
import "bytes"
bs := []byte("something")
r := bytes.NewReader(bs)
Getting a Reader from a string
s := "some string"
reader := strings.NewReader(s)
Writer
Files opened with os.Create()
implement io.Writer
.
Closer
Files opened with os.Create()
implement io.Closer
.
ReadCloser
An interface that exposes basic Read()
and Close()
methods. The http.Request
body is a ReadCloser
. See Reader
.