# 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%#vfor the Go-syntax representation%tfor a boolean; it formats a value astrueorfalse%bfor an integer; it formats the integer to its binary representation%cfor 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%cmatches 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 asPrintbut writes tow.func Fprintln(w io.Writer, a...interface{}) (n int, err error): same asPrintlnbut writes tow.func Fprintf(w io.Writer, a...interface{}) (n int, err error): same asPrintfbut writes tow.func Fscan(r io.Reader, a...interface{}) (n int, err error): same asFscanbut scans fromr.func Fscanln(r io.Reader, a...interface{}) (n int, err error): same asFscanlnbut scans fromr.func Fscanf(r io.Reader, a...interface{}) (n int, err error): same asFscanfbut 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)interpretssas an integer in basebase, from2to36; and with typebitSize, whereby0meansint,8meansint8,16meansint16,32meansint32, and64meansint64. ParseUintis 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) stringreturns"true"or"false"according tob.func FormatFloat(f float64, fmt vyte, prec, bitSize int) stringreturns a string representation offwith formatfmtand with precisionprec.func FormatInt(i int64, base int) stringreturns the string representation ofiin basebase.func FormatUint(i uint, base int) stringis the same asFormatInt, but it takes a unsigned integer.func Itoa(i int) stringis shorthand forFormatInt(int64(i), 10), so it gives a decimal representation ofias a string.
# 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.
# Next up
Before diving deep into Cosmos, take a look at Go's built-in concurrency by exploring Goroutine and channels in the next section.