Go: Hello, World!

Go is simple. For example, it has 25 keywords, which is less than C (37) and C++ (84). Let’s start using Go with a simple “Hello, World!” program.

Environment

  • macOS Catalina, Version 10.15.7.

Install Go

  1. Downland Go from https://golang.org/dl. Here the binary release is used.

    $ wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz
    
  2. Extract the archive into /usr/local.

    $ sudo tar -C /usr/local -xzvf go1.17.1.linux-amd64.tar.gz
    
  3. Add the Go binary to the PATH environment variable.

    $ export PATH=$PATH:/usr/local/go/bin
    

    To make such change persistent, add the above command to ~/.profile.

  4. Verify the installation with the Go version.

    $ go version
    

Hello, World!

Create a new Go file (e.g., hello.go) and add the following content.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

To run this program, simply run:

$ go run hello.go
Hello, World!

We can also build the Go file and then run the binary.

$ go build hello.go 
$ ./hello 
Hello, World!

Contents