# Standard Packages in Go
Like other languages, Go offers a lot of useful standard packages. You will look at:
- fmt
- strconv
- errors
# fmt
fmt (format) implements are formatted I/O - you covered a good number of its verbs in module 1. Now you will see a range of additional useful verbs:
%%
for the percent sign%#v
for the Go-syntax representation%t
for a boolean; it formats a value astrue
orfalse
%b
for an integer; it formats the integer to its binary representation%c
for an integer; it formats the integer to its corresponding Unicode character
If you use an invalid verb, you will get a string beginning with "%!"
and a description.
To write to stdout
:
func Print(a ...interface{}) (n int, err error)
formats with default formats.func Println(a ...interface{}) (n int, err error)
formats with default formats and appends a newline.func Printf(format string, a ...interface{}) (n int, err error)
formats with given format.
These three functions return the number of bytes written and any applicable error.
To read from stdin
:
func Scan(a ...interface{}) (n int, err error)
parses using default formats. Values are space-separated. Newlines count as spaces.func Scanln(a ...interface{}) (n int, err error)
parses using default formats and stops at a newline.func Scanf(format string, a ...interface{}) (n int, err error)
parses using the given format. The verb%c
matches every rune (space, tab, newline, etc.).
These three functions return the number of items scanned and an error if the count of items parsed is smaller than the count of arguments.
Perhaps you noticed a kind of function declaration we did not speak about: the variadic function can be called with any number of arguments. You can iterate over the arguments as follows:
There are formatting functions on io.Writer and io.Reader, so we also have:
func Fprint(w io.Writer, a...interface{}) (n int, err error)
: same asPrint
but writes tow
.func Fprintln(w io.Writer, a...interface{}) (n int, err error)
: same asPrintln
but writes tow
.func Fprintf(w io.Writer, a...interface{}) (n int, err error)
: same asPrintf
but writes tow
.func Fscan(r io.Reader, a...interface{}) (n int, err error)
: same asFscan
but scans fromr
.func Fscanln(r io.Reader, a...interface{}) (n int, err error)
: same asFscanln
but scans fromr
.func Fscanf(r io.Reader, a...interface{}) (n int, err error)
: same asFscanf
but scans fromr
.
io.Writer
is the interface that declares the Write
method. io.Reader
is also an interface and it declares the Read
method.
The functions Sprint
, Sprintln
, and Sprintf
are similar to Print
, Println
, and Printf
, with the difference that they return a string
instead of writing to stdout
.
The functions Sscan
, Scanln
, and Sscanf
are similar to Fscan
, Fscanln
, and Fscanf
with the difference that they scan from a string
given as an argument.
The function Errorf
formats according to a format and returns it as an error.
# strconv
The package strconv offers conversions to and from strings of basic data types.
# Convert from string
Start with an example for using strconv (opens new window):
- The function
ParseInt(s string, base int, bitSize int) (i int64, err error)
interpretss
as an integer in basebase
, from2
to36
; and with typebitSize
, whereby0
meansint
,8
meansint8
,16
meansint16
,32
meansint32
, and64
meansint64
. ParseUint
is similar, with the difference that it returns an unsigned integer.- The
func Atoi(s string) (int, error)
returnsParseInt(s, 10, 0)
as typeint
.
# Convert to string
func FormatBool(b bool) string
returns"true"
or"false"
according tob
.func FormatFloat(f float64, fmt vyte, prec, bitSize int) string
returns a string representation off
with formatfmt
and with precisionprec
.func FormatInt(i int64, base int) string
returns the string representation ofi
in basebase
.func FormatUint(i uint, base int) string
is the same asFormatInt
, but it takes a unsigned integer.func Itoa(i int) string
is shorthand forFormatInt(int64(i), 10)
, so it gives a decimal representation ofi
as a string.
Further reading:
# Errors
In Go, errors are values. The convention is that the last return value of a function is the error. This is the entire code of errors package:
You use func New(text string) error
to create an error. An example is as follows:
In this case, nil
means no error. For best practice, you should always check for errors. However, take the time to review the following list, because Go error handling differs from other languages.
Further reading:
To summarize, this section has explored:
- The fmt (format) standard package in increased detail, including a range of useful verbs and function declarations.
- The strconv package, which offers conversions to and from strings of basic data types.
- Error values, which by convention are the last return value of a function.