Installing Go and your first program
Go (also called Golang) is a language created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson. It was born to solve a very concrete problem: writing fast, maintainable servers without waiting minutes for compilation. Today it is used in tools such as Docker and Kubernetes, in web APIs, in command-line utilities and in cloud systems. It is a small language with few reserved words, which makes it ideal for learning from zero.
Installation
Go to go.dev/dl, download the installer for your operating system (Windows, macOS or Linux) and run it. In this course we use Go 1.23, but everything we will see works the same in later versions. When it finishes, open a new terminal, because the old one does not know the updated PATH variable yet, and type the verification command shown below. If you see a version number, you are ready. If the terminal says the command does not exist, close and reopen the terminal or check that Go's bin folder is in your PATH.
go version
# go version go1.23.0 windows/amd64The Hello, world program
Create a folder called hello, enter it and run go mod init hello. That command creates the go.mod file, which identifies your project as a module; we will study it in depth in week 6, for now it is enough to know that it is required. Inside the folder create a file main.go with the content below. Notice three pieces: the line package main declares that this file belongs to the main package; import brings the fmt package from the standard library; and func main is the function where every executable program starts running.
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}go run and go build
Go is a compiled language: the source code is translated into an executable file. For quick tests there is go run, which compiles into a temporary directory and runs immediately without leaving files behind. When you want the final executable use go build, which generates a binary in the current folder (hello.exe on Windows). That binary is self-contained: you can copy it to another machine with the same operating system and it works without installing Go. This easy distribution is one of the reasons for its popularity.
go run main.go
go build
./hello- go version: shows the installed version.
- go run: compiles and runs without saving the binary.
- go build: compiles and leaves the executable.
- go fmt ./...: formats all the code with the official style.
- go vet ./...: looks for suspicious mistakes that compile but smell wrong.
Common mistakes
The first typical mistake is importing a package and not using it: Go refuses to compile and says the import is declared and not used. The same happens with unused local variables. It is annoying at first, but it prevents dead code. The second mistake is putting the opening brace on a new line; in Go it must stay at the end of the same line as func main(). The third is writing fmt.println in lowercase: in Go, names that start with an uppercase letter are public and lowercase ones are private to the package, so only Println exists for you.
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen