Tarkistetaan oppitunnin käyttöoikeutta...

Your First Go Program

Set Up Go and Run Your First Program

Video ja luettava10 minEsikatselu

Katselet ilmaista esikatseluoppituntia.

Goal

By the end of this lesson, Go is installed on your computer. You will write a small program, run it, and turn it into a program file that runs on its own.

What you will learn

  • How to install Go and check that it works
  • What a Go module is and how to create one
  • The three parts every Go program starts with: package, import, and func main
  • The difference between go run and go build

Why we start here

In this course, you build a real tool called Tasker. First it is a command-line tool, and later it is a web API. Every Go project, small or large, starts with the same steps you learn in this lesson. You will repeat them many times, so it is worth learning them well.

Step 1: Install Go

  1. Open https://go.dev/dl in your browser.
  2. Download the installer for your operating system (Windows, macOS, or Linux).
  3. Run the installer and follow the steps.

When the installer finishes, open a new terminal window. Run this command:

go version

You should see output similar to this:

go version go1.xx.x linux/amd64

The numbers depend on your version and your computer. If you see a version, Go is ready.

If the command is not found: close the terminal and open a new one. The installer changes your system settings, and old terminal windows do not see the change.

Step 2: Create a project folder

Make a folder for this lesson and move into it:

mkdir hello
cd hello

Step 3: Create a module

Run this command inside the hello folder:

go mod init hello

Go creates one file, go.mod. Open it. It looks like this:

module hello

go 1.xx

A module is a Go project. The go.mod file stores two things:

  • The module name (hello). Other code uses this name to find your code.
  • The Go version that the project expects.

Later, go.mod also lists the libraries your project uses, such as Cobra and Fiber.

Step 4: Write the program

Create a file named main.go in the hello folder. Add this code:

package main

import "fmt"

func main() {
    fmt.Println("Hello from Tasker!")
}

This program has three parts.

package main Every Go file belongs to a package. A package is a group of Go files in the same folder. The name main is special. It tells Go: "This package is a program you can run."

import "fmt" This line loads the fmt package from the Go standard library. The standard library is a large set of packages that comes with Go. The fmt package prints and formats text.

func main() This is the function where your program starts. When the program runs, Go calls main first. When main ends, the program ends.

fmt.Println prints text and then starts a new line. The name has two parts: the package (fmt) and the function (Println). The dot connects them.

Step 5: Run the program

Run this command:

go run .

Output:

Hello from Tasker!

The . means "the current folder." go run compiles your code, runs it, and then deletes the compiled program. Use go run while you write and test code.

Step 6: Build a program file

Run this command:

go build

Go creates a new file in the folder. On macOS and Linux it is named hello. On Windows it is named hello.exe. This file is your compiled program. Run it:

./hello

On Windows, run this instead:

.\hello.exe

Output:

Hello from Tasker!

This file contains everything the program needs. You can copy it to another computer with the same operating system, and it runs there. That computer does not need Go installed. This is one reason Go is popular for command-line tools and servers.

Why it works: Go is a compiled language

Go translates your code into machine code before the program runs. This step is called compiling. Machine code is the language your computer's processor understands.

  • go run compiles the code and runs it once.
  • go build compiles the code and keeps the result as a file.

Compiling also checks your code for many mistakes before the program starts. You will see this in the exercises.

Exercises

  1. Change the message. Make the program print your name. Run it with go run ..
  2. Print two lines. Call fmt.Println twice in main. Each call prints its own line.
  3. Break it on purpose. Remove the import "fmt" line and run go run .. Read the error. Which line does Go point to? Put the import back.
  4. Break it another way. Add import "os" below the fmt import, but do not use os anywhere. Run the program. Go refuses to compile code that imports a package it does not use. Why might that be a good rule?
  5. Build and compare. Run go build, then check the size of the program file. Go programs include everything they need inside one file, so the file is bigger than your source code.

Checkpoint

You are ready for the next lesson if you can:

  • Run go version and see a version number
  • Explain what go.mod is for
  • Name the three parts of main.go and say what each one does
  • Explain when to use go run and when to use go build