The goal of this exercise is to set-up your Go development environment and practice basic commands using the go
executable.
- Download Go.
- Consult your package manager. If it provides a up-to-date version of Go install it using it.
- MacOS [brew]:
$ brew install go
- Ubuntu [apt]:
$ sudo apt install golang-go
- Arch [pacman]:
$ sudo pacman -S go
- Windows [cocolatey]:
$ choco install golang
- MacOS [brew]:
- If your package manager does not provide you with Go package or if it is not up-to-date, install Go manually using the official guide.
- Consult your package manager. If it provides a up-to-date version of Go install it using it.
- Verify your installation.
$ go version
- Choose your IDE.
- If you are fan of JetBrains tools you can use their dedicated IDE for Go, GoLand.
- If you prefer VS Code just install the VS Code Go plugin.
- If you are old school or you just like staying in your terminal using vim/neovim with vim-go is also an excelent choice.
The go command and the tools it invokes consult environment variables for configuration. We will just take a quick glimpse at them.
- Go downloads and installs all packages to a directory specified by the GOPATH environment variable.
- To view the currently set GOPATH execute
$ go env GOPATH
. - By default:
- $HOME/go on Unix,
- %USERPROFILE%\go on Windows
- You can change the GOPATH by setting it explicitly.
- E.g. add
export GOPATH=$HOME/.go
to your bashrc or zshrc.
- E.g. add
- To view the currently set GOPATH execute
- Go SDK can be specified by the GOROOT variable.
- You do not need to set this variable unless you plan to use different Go version.
- To view the current state of all of your Go environment variables execute:
$ go env
.- Run it and briefly skim over the output.
- To view all configurable variables and their meaning execute:
$ go help environment
.
In this last section, we will create a new Go project and practice basic go
commands.
- Create a new directory for our code and move into it.
- The
go.mod
file represents a go module. It is similar to files like:*.csproj
in C#pom.xml
in Java Maven projectCargo.toml
in Rustpackage.json
in Javascript
- To create a new module execute:
$ go mod init <name-of-module>
.- You can name your module whatever you would like, e.g. "my-project". However, it is a common practice to name your module the same way as the repository in which it will be hosted. E.g. if you wanted to host some project on GitHub, you would name the module "github.com/[my-account]/[my-project]".
- The
go.mod
file specifies basic information about your project like it's name, the Go version it uses and list of it's dependencies. - To learn more about the
go.mod
execute:$ go help go.mod
- To learn more about go modules execute:
$ go help modules
- Create a new file named
main.go
which will serve as the projects entrypoint and insert the following code into it.- This is just a basic hello world program.
package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}
- To execute the program run:
$ go run .
.- This compiles your program and executes it immediately.
- The current directory has to contain the
package main
and themain
function (the file it-self does not have to be calledmain.go
but it is a good convention).
- Try changing the package name from
main
tomain2
and run the$ go run .
again.- Compiler should throw an error:
package my-project is not a main package
- Compiler should throw an error:
- Changing the name of the
main()
function results in a similar error.runtime.main_main·f: function main is undeclared in the main package
- Change the source code back to the functioning state as specified above.
- To build your program execute:
$ go build .
.- This command compiles your program and generates an executable binary.
- The binary generates into the current directory and is named after the project's name, as specified by
go.mod
. - The change the output name or directory you can use the
-o
switch like so:$ go build -o bin/my-binary .
.
- Change the
main.go
source code to the following snippet:- This is definitely not a well-formatted code. Luckily, Go ships with a default formatter.
package main
import"fmt"
func main(){
fmt.Println("Hello world!")}
- To format all go files in the current directory execute:
go fmt .
.- This will change the source code to follow the uniform Go style format.
- Note that most IDEs and editors automatically execute the
fmt
for you.
- To format all go files in the source tree you can use the wildcard pattern:
go fmt ./...
.- The
./...
can be used with many other Go commands likego list
,go get
,go test
etc.
- The
- Let's try to add some dependencies to our project.
- We will replace our "Hello world!" string with a constant from "github.com/course-go/code" repository.
- To add the
course-go code
repository to the project's dependencies execute:go get github.com/course-go/code
- Note that
course-go/code
was added to thego.mod
file with a check-sum of it ingo.sum
. - The source code of the repository got downloaded to the $GOPATH/pkg directory.
- Feel free to check it out, to get a better sense of how it works.
- We can now use the exported code from
course-go/code
like this:
package main
import (
"fmt"
"github.com/course-go/code/pkg/hello"
)
func main() {
fmt.Println(hello.World)
}
- We will install the
go present
tool which is used in the course for presenting slides.- To install it, simply run
go install golang.org/x/tools/cmd/present@latest
.- Notice that we have to specify the version (in this case
latest
) when we are not in a Go module. – This downloads the source code, builds it and saves the executable binary. - The executable will be saved to directory specified by $GOBIN which defaults to $GOPATH/bin.
- Execute:
go help install
to learn more. - Do not forget to add this directory to your path for convenience like so:
export PATH="$GOPATH/bin:$PATH"
- Notice that we have to specify the version (in this case
- To install it, simply run
- Clone the lectures repository.
- Move into it and execute the present binary:
present
. - This should start a web server where you can check-out the slides.