Go Package io: Difference between revisions
No edit summary |
|||
(26 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
* [[Go_Language_Modularization#io|Standard Library]] | * [[Go_Language_Modularization#io|Standard Library]] | ||
* [[File Operations in Go#Overview|File Operations in Go]] | |||
=Overview= | =Overview= | ||
Line 12: | Line 13: | ||
The <tt>io</tt> package consists of a few functions, but mostly interfaces used by other packages. | The <tt>io</tt> package consists of a few functions, but mostly interfaces used by other packages. | ||
=<tt> | =<tt>io.EOF</tt>= | ||
The error returned by I/O operations that encounter the end of file. It can be tested with: | |||
= | <syntaxhighlight lang='go'> | ||
_, err := ... | |||
if errors.Is(err, io.EOF) { | |||
// is EOF | |||
... | |||
} | |||
</syntaxhighlight> | |||
=<span id='Reader'></span><tt>io.Reader</tt>= | |||
{{External|https://golang.org/pkg/io/#Reader}} | |||
An interface that exposes <code>Read()</code>. | |||
<code>Reader</code>s can be used to unmarshall JSON into structs as shown here: {{Internal|JSON_in_Go#With_a_Reader|Unmarshalling JSON from a Reader into a struct}} | |||
Files opened with <code>[[Go_Package_os#os.Create.28.29|os.Create()]]</code> implement <code>io.Reader</code>. | |||
==<span id='WBSR'></span>Wrapping a <tt>[]byte</tt> into a <tt>Reader</tt>== | |||
A <code>[]byte</code> can be wrapped into a <code>Reader</code> with: | |||
<syntaxhighlight lang='go'> | |||
import "bytes" | |||
bs := []byte("something") | |||
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> | |||
=<span id='Writer'></span><tt>io.Writer</tt>= | |||
{{External|https://golang.org/pkg/io/#Writer}} | |||
<code>io.Writer</code> is the interface that wraps the basic Write method: | |||
<syntaxhighlight lang='go'> | |||
Write(p []byte) (n int, err error) | |||
</syntaxhighlight> | |||
<code>Write()</code> writes the given bytes to the underlying data stream. | |||
Files opened with <code>[[Go_Package_os#os.Create.28.29|os.Create()]]</code> implement <code>io.Writer</code>. | |||
==Capturing the Content Written into a Writer and Converting to String== | |||
<code>bytes.Buffer</code> implements <code>io.Writer</code> so a <code>bytes.Buffer</code> instance can be used to be written into and get the string representation. The following example dumps the content of a <code>net/http.Request</code> into a string, identically with how it will be sent over the wire: | |||
<syntaxhighlight lang='go'> | |||
var r = &http.Request{...} | |||
// Capturing the Content Written into a Writer and Converting to String. | |||
b := &bytes.Buffer{} | |||
if err := r.Write(b); err != nil { ... } | |||
s := b.String() | |||
</syntaxhighlight> | |||
=<tt>Closer</tt>= | |||
{{External|https://golang.org/pkg/io/#Closer}} | |||
Files opened with <code>[[Go_Package_os#os.Create.28.29|os.Create()]]</code> implement <code>io.Closer</code>. | |||
=<tt>ReadCloser</tt>= | |||
An interface that exposes basic <code>Read()</code> and <code>Close()</code> methods. The <code>http.Request</code> body is a <code>ReadCloser</code>. See <code>[[#Reader|Reader]]</code>. |
Latest revision as of 02:10, 21 July 2024
External
Internal
Overview
The io package consists of a few functions, but mostly interfaces used by other packages.
io.EOF
The error returned by I/O operations that encounter the end of file. It can be tested with:
_, err := ...
if errors.Is(err, io.EOF) {
// is EOF
...
}
io.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)
io.Writer
io.Writer
is the interface that wraps the basic Write method:
Write(p []byte) (n int, err error)
Write()
writes the given bytes to the underlying data stream.
Files opened with os.Create()
implement io.Writer
.
Capturing the Content Written into a Writer and Converting to String
bytes.Buffer
implements io.Writer
so a bytes.Buffer
instance can be used to be written into and get the string representation. The following example dumps the content of a net/http.Request
into a string, identically with how it will be sent over the wire:
var r = &http.Request{...}
// Capturing the Content Written into a Writer and Converting to String.
b := &bytes.Buffer{}
if err := r.Write(b); err != nil { ... }
s := b.String()
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
.