Welcome to this free digital book on the Go programming language! It is divided into several topics to provide a complete overview of Go, its features, and what can be created with it.

This book tries to provide a step-by-step tutorial for complete beginners - yet it can be used by developers with existing Go experience to refresh knowledge or learn new concepts.

No long talk. Let’s dive into this amazing language!

Installing Go

Installing the Go programming language can be done here: https://go.dev/doc/install

Once you have completed the installation, type the following into your terminal to see if Go is working:

go version 

Ideally, an output like go version go1.22.2 darwin/amd64should appear. You are good to go!

Hello world in Go

Once your environment is set up, you’re ready to write your first Go program. Here’s how you can write the classic “Hello World”:

  1. Create a New File: Inside your directory of choice, create a new file named hello.go.
  2. Write the Code: Open hello.go in your editor and type the following Go code:
package main
 
import "fmt"
 
func main() {
    fmt.Println("Hello, world!")
}
  1. **Let’s understand what is happening here:
    • package main: This line defines the package name. Every Go program starts with a package declaration. Programs that are executable start with package main.
    • import "fmt": This line tells the Go compiler to include the fmt package, which contains functions for formatted I/O operations (like printing text to the console).
    • func main(): The main function is the entry point of the program. When you run the Go program, the code inside the main function executes.
    • fmt.Println("Hello, world!"): This line outputs the string “Hello, world!” to the console. Println is a function from the fmt package that prints its arguments followed by a new line.

Running the Program

To run your Go program, follow these steps:

  1. Open a Terminal/Command Prompt: Navigate to your directory where your hello.go file is located.
  2. Run the Program: Type go run hello.go and press Enter. The Go toolchain will compile and execute your program, and you should see “Hello, world!” printed in the terminal.

Great! You executed your first program in Go.

Next Chapter: Datatypes in Go