Go Package io: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Go Concepts")
 
 
(30 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://golang.org/pkg/io/
* https://golang.org/pkg/io/ioutil/
=Internal=
=Internal=


* [[go Concepts#Packages|Go Concepts]]
* [[Go_Language_Modularization#io|Standard Library]]
* [[File Operations in Go#Overview|File Operations in Go]]
 
=Overview=
 
The <tt>io</tt> package consists of a few functions, but mostly interfaces used by other packages.
 
=<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>
 
=<tt>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>
 
=<tt>Writer</tt>=
{{External|https://golang.org/pkg/io/#Writer}}
 
Files opened with <code>[[Go_Package_os#os.Create.28.29|os.Create()]]</code> implement <code>io.Writer</code>.
 
=<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 21:17, 29 February 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 
  ...
}

Reader

https://golang.org/pkg/io/#Reader

An interface that exposes Read().

Readers can be used to unmarshall JSON into structs as shown here:

Unmarshalling JSON from a Reader into a struct

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

https://golang.org/pkg/io/#Writer

Files opened with os.Create() implement io.Writer.

Closer

https://golang.org/pkg/io/#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.