Check Ubuntu version
First, run this command to make sure what version of Ubuntu you have:
lsb_release -a
Output example:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
Install GoLang
Download the latest GoLang archive:
curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
Check SHA256 Checksum just in case:
sha256sum go1.17.3.linux-amd64.tar.gz
Extract everything to the usr/local
directory:
sudo tar -C /usr/local -xvf go1.17.3.linux-amd64.tar.gz
Update PATH
variable in ~/.profile
file:
sudo nano ~/.profile
Add new row with export
at the end of the ~/.profile
file:
export PATH=$PATH:/usr/local/go/bin
Save the changes and exit nano
editor. Now we have to refresh your profile. Run this command:
source ~/.profile
Installation and setup is done. Let's check if everything works.
Make sure everything works
We will check Go version and create and run a simple program.
Check the version
Run this command to check Go version:
go version
Output:
go version go1.17.3 linux/amd64
Create and run 'Hello, World!'
Let's create a simple Go program and run it.
Create a directory and switch to it.
mkdir hello_go
cd hello_go
Now we have to create go.mod
file with the go mod init
command:
go mod init test/hello_go
Create a file where we'll write the program code in Go.
nano hello.go
Copy this example code to the file and save changes.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save the changes and exit nano
editor.
Run the program:
go run .
Output:
Hello, World!
Done!
Top comments (1)
It's much easier to use apt:
or snap: