Blog / Linux/ Guide to Installing and Configuring Go on Debian 10

Guide to Installing and Configuring Go on Debian 10

Debian 10 安装与配置 Go 语言开发环境指南

Introduction to Go

Go (often called Golang) is a modern, open-source programming language developed by Google. It is designed to make software development secure, fast, and easy to learn, helping developers build reliable and efficient software.

This guide will walk you through installing and configuring the Go development environment on Debian 10, and compiling and running the classic "Hello, World!" program.

Installing Go

First, update your system's package list and install necessary tools like curl. Then, download and install Go. The version used in the original post (1.12.7) is outdated. It is recommended to install a newer version for better performance and security. The following steps use Go 1.21.6 as an example (visit the official Go download page for the latest version link).

sudo apt update
sudo apt install curl -y
curl -O https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz

Configuring Environment Variables

Go requires setting a few key environment variables: GOROOT (the Go installation directory), GOPATH (your workspace directory), and updating the PATH.

Edit your shell profile file (e.g., ~/.profile or ~/.bashrc):

nano ~/.profile

Add the following lines to the end of the file:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Note: Modern Go versions (1.11+) introduced module support, making GOPATH non-mandatory. However, setting it is still a good practice. Setting GOPATH to $HOME/go is the common convention.

Apply the changes:

source ~/.profile

Verify the installation:

go version

If successful, you will see output similar to go version go1.21.6 linux/amd64.

Compiling and Running "Hello, World!"

Now, let's create a simple Go program.

  1. Create a directory for your project (using Go modules, it doesn't need to be strictly under GOPATH):
    mkdir -p ~/hello-world
    cd ~/hello-world
  2. Initialize a new Go module:
    go mod init hello/world
  3. Create the program file hello.go:
    nano hello.go
  4. Enter the following code into hello.go:
    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World!")
    }
  5. Run the program to verify:
    go run hello.go

    If everything is correct, the terminal will output: Hello, World!.

Optional: Compile to an Executable

You can also compile the program into a standalone binary:

go build -o hello hello.go
./hello

This will generate an executable file named hello in the current directory. Running it will also output Hello, World!.

Summary

You have successfully installed the latest version of Go on Debian 10, configured the environment variables, and created and run your first Go program using Go modules. The Go module system simplifies dependency management and is the standard for modern Go development. Next, you can explore Go's standard library and rich ecosystem of third-party packages to build more complex applications.

Post a Comment

Your email will not be published. Required fields are marked with *.