1. How is go.mod file generated?

Run go mod init command on your terminal. The command accepts the module path as an argument. The module path refers to the location where the module may be found. Put it another way, it is the import path used for the root directory.

Running the command will generate the following message in the standard output.

go: creating new go.mod: module example.com/greetings

Inside the go.mod file, it will contain 3 pieces of information: the module name, the Go version, and dependency requirements with specific versions.

module example.com/greetings

go 1.15

require rsc.io/quote v1.5.2


2. What does go build do?

$ go help build

Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.

Let’s say we have a single directory called hello. Inside the directory, we have two files, go.mod and hello.go that contains a main package. Then we run go build inside the hello directory.

For main packages, the default behavior of go build is to write the resulting executable to an output file named after the first source file. In our case, hello. When you run ./hello in your terminal, whatever expected message will be returned.

For non-main packages, the command builds our package then discards the results.


3. What does each package do in this particular tutorial? (i.e., fmt, log, error)

  • fmt: a package that contains functions for handling input and output text
  • log: a logging pacakge that defines a type, Logger, along with helper functions including Print, Fatal, and Panic.
  • errors: a package that contains functions for manipulating errors. Functions include New, Unwrap, Is, and As.


4. What is a Go slice?

A dynamically sized collection that can only store items of a similar type


References