Chapter 11. The Standard Library
One of the best parts of developing with Go is being able to take advantage of its standard library. Like Python, it has a âbatteries includedâ philosophy, providing many of the tools that you need to build an application. Since Go is a relatively new language, it ships with a library that is focused on problems faced in modern programming environments.
We canât cover all of the standard library packages, and luckily, we donât have to, as there are many excellent sources of information on the standard library, starting with the documentation. Instead, weâll focus on several of the most important packages and how their design and use demonstrate the principles of idiomatic Go. Some packages (errors
, sync
, context
, testing
, reflect
, and unsafe
) are covered in their own chapters. In this chapter, weâll look at Goâs built-in support for I/O, time, JSON, and HTTP.
io and Friends
For a program to be useful, it needs to read in and write out data. The heart of Goâs input/output philosophy can be found in the io
package. In particular, two interfaces defined in this package are probably the second and third most-used interfaces in Go: io.Reader
and io.Writer
.
Note
Whatâs number one? Thatâd be error
, which we already looked at in
Chapter 8.
Both io.Reader
and io.Writer
define a single method:
type
Reader
interface
{
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
}
type
Writer
interface
{
Write
(
p
[]
byte
)
(
n
int
,
err
error
)
}
The Write
method ...
Get Learning Go now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.