Technical skill

Go

Give the computer a clear set of steps

A program is a set of instructions that tells a computer what to do. You might use one to read a file, check its contents, and print a result without repeating those steps by hand. Go is a language for writing such programs. It is useful when the job has several steps or needs to be easy to run again on another computer.

Here is a small Go program that reads the Linux distribution's release file and prints it. The file holds basic information such as the distribution name and version.

package main

import (
    "fmt"
    "os"
)

func main() {
    data, err := os.ReadFile("/etc/os-release")
    if err != nil {
        fmt.Fprintf(os.Stderr, "read release information: %v\n", err)
        os.Exit(1)
    }
    fmt.Print(string(data))
}

The first lines make the printing and file-reading tools available. main is where this program starts. os.ReadFile returns the file's contents and an error value. If reading fails, the program writes a message to the error output and exits with a failure status. Otherwise, it prints the contents. This lets another program or a person distinguish a successful read from a failure.

Organize a Go program

Go instructions are saved in source files, usually ending in .go. A package is a group of source files that belong together. The special package name main marks a program that can be run. A module is the project unit that records which packages belong together and which outside libraries they use. Start one in a new directory with go mod init example.com/release-check; Go writes its settings to go.mod. Choose a module path that fits the project, especially if other projects will import it.

Go assigns each value a kind, called its type, and checks that operations use compatible types. For example, file contents are bytes, while the example converts them to text before printing. This catches some mismatches before the program runs. A name that starts with a capital letter can be used by other packages; a lowercase name is kept inside its package.

Many Go operations return both a result and an error. The program must decide what to do with the error. It may retry, explain the problem, or stop. Ignoring an error can make a program appear to have succeeded when it did not.

Run and check it

Save the example as main.go inside the module directory. Use go run . while trying it out; use go build to compile a reusable program file. gofmt formats Go source, go test ./... runs tests in the module's packages, and go build checks that the program compiles.

The example expects /etc/os-release, a file commonly provided by Linux distributions. It describes the distribution, not the kernel version or installed packages. Check those separately when needed. Try both a readable file and a missing or unreadable one. Before a program handles real data, review every path it reads or changes and every external command it starts.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Web Backend Developer preview
image descriptionWeb Backend Developer
by Infinite Education
2092 users
Skillset PostgreSQL DBA preview
image descriptionPostgreSQL DBA
by Artem Naumenko (fb)
1630 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful