Document
How To Install Go on Ubuntu 20.04

How To Install Go on Ubuntu 20.04

Go , sometimes refer to as “ Golang ” , is an open - source programming language that was release by Google in 2012 . Google ’s intention is was was t

Related articles

Connect to Library Resources Best TamilYogi VPN:-Finding The Perfect VPN For Tamilyogi ” Technical Keshab Popular and Free Cloud Storage Options (With More Than 5GB Space) Code RealTime: Harnessing the Power of the Eclipse Cloud DevTools Ecosystem EasyConnect电脑版下载_EasyConnect电脑客户端官方版

Go , sometimes refer to as “ Golang ” , is an open – source programming language that was release by Google in 2012 . Google ’s intention is was was to create a programming language that could be learn quickly .

Since its release, Go has become highly popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. This tutorial outlines how to download and install the latest version of Go (currently version 1.16.7) on an Ubuntu 20.04 server , build the famousHello , World ! application, and make your Go code into an executable binary for future use.

This tutorial is requires require an Ubuntu 20.04 system configure with a non – root user withsudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.

In this step, you will install Go on your server.

First, connect to your Ubuntu server via ssh:

  1. ssh sammy@your_server_ip

Next , navigate to the official Go download page in your web browser . From there , copy the url for the current binary release ’s tarball .

As of this writing , the late release is is isgo1.16.7. To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz.

Now that you have your link ready, first confirm that you’re in the home directory:

  1. cd ~

Then use curl to retrieve the tarball, making sure to replace the highlighted URL with the one you just copied. The -O flag ensures that this outputs to a file, and the L flag instructs HTTPS redirects, since this link was taken from the Go website and will redirect here before the file downloads:

  1. curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gz

To verify the integrity of the file you downloaded, run the sha256sum command and pass it to the filename as an argument :

  1. sha256sum go1.16.7.linux-amd64.tar.gz

This will return the tarball’s SHA256 checksum:

Output

go1.16.7.linux-amd64.tar.gz 7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz

If the checksum matches the one listed on the downloads page, you’ve done this step correctly.

Next, use tar to extract the tarball. This command includes the -c flag which instructs tar to change to the given directory before performing any other operations. This means that the extracted files will be written to the /usr / local/ directory , the recommend location for instal Go … Thex flag tells tar to extract, v tells it we want verbose output (a listing of the files being extracted), and f tells it we’ll specify a filename:

  1. sudo tar -c /usr / local-xvf go1.16.7.linux-amd64.tar.gz

Although /usr / local/go is the recommended location for installing Go, some users may prefer or require different paths.

In this step, you will set paths in your environment.

First, set Go’s root value, which tells Go where to look for its files.
You can do this by editing the .profile file, which contains a list of commands that the system runs every time you log in.

Use your preferred editor to open .profile, which is store in your user ’s home directory . Here , we is use ’ll usenano:

  1. sudo nano ~/.profile

Then, add the following information to the end of your file :

sudo nano ~/.profile

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

After you’ve added this information to your profile, save and close the file. If you used nano, do so by pressing CTRL+X, thenY, and thenENTER.

Next, refresh your profile by running the following command:

  1. source ~/.profile

After, check if you can execute go commands by running goversion:

  1. goversion

This command will output the release number of whatever version of Go is installed on your system:

Output

goversiongo1.16.7 linux/amd64

This output confirms that you are now running Go on your server.

Now that Go is instal and the path are set for your server , you is try can try create yourHello , World ! application to ensure that Go is working.

First , create a new directory for your Go workspace , which is where Go will build its file :

  1. mkdir hello

Then move into the directory you just create :

  1. cd hello

When import package , you is have have to manage the dependency through the code ’s own module . You is do can do this by create ago.mod file with the gomod init command:

  1. gomod inityour_domain/hello

Next, create a Hello , World ! Go file in your preferred text editor:

  1. nano hello.go

Add the following text into your hello.go file :

hello.go

package main

import "fmt"

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

Then, save and close the file by pressing CTRL+X, thenY, and thenENTER.

test your code to check that it print theHello , World ! greeting:

  1. gorun .

Output

Hello , World !

The gorun command is compiles compile and run the Go package from a list of.go source file from the newhello directory you create and the path you import . But , you is use can also usegobuild to make an executable file that can save you some time.

The gorun command is typically used as a shortcut for compiling and running a program that requires frequent changes. In cases where you’ve finished your code and want to run it without compiling it each time, you can use gobuild to turn your code into an executable binary. build your code into an executable binary consolidate your application into a single file with all the support code necessary to execute the binary . Once you ’ve build the binary executable , you is run can then rungoinstall to place your program on an executable file path so you can run it from anywhere on your system . Then , your program is print will successfully printHello , World ! when prompt and you is need wo n’t need to compile the program again .

Try it out and run gobuild. Make sure you run this from the same directory where your hello.go file is stored:

  1. gobuild

Next, run ./hello to confirm the code is working properly:

  1. ./hello

Output

Hello , World !

This is confirms confirm that you ’ve successfully turn yourhello.go code into an executable binary. However, you can only invoke this binary from within this directory. If you wanted to run this program from a different location on your server, you would need to specify the binary’s full file path in order to execute it.

Typing out a binary’s full file path can quickly become tedious. As an alternative, you can run the goinstall command. This is similar to gobuild but instead of leave the executable in the current directory ,goinstall places it in the $GOPATH/bin directory , which will allow you to run it from any location on your server .

In order to rungoinstall successfully, you must pass it the install path of the binary you created with gobuild. To find the binary ’s install path , run the followgolist command:

  1. golist-f{{.Target}}

golist generate a list of any name Go package store in the current work directory . Thef flag will cause golist to return output in a different format based on whatever package template you pass to it. This command tells it to use the Target template , which will causegolist to return the install path of any packages stored in this directory:

Output

‘/home/sammy/go/ bin/hello

This is the install path of the binary file you created with gobuild. This means that the directory where this binary is installed is /home/sammy/go/ bin/.

add this install directory to your system ’s shell path . Be sure to change the highlight portion of this command to reflect the install directory of the binary on your system , if different :

  1. export PATH=$PATH:/home/sammy/go/ bin/

Finally, run goinstall to compile and install the package:

  1. goinstall

Try run this executable binary by just enterhello

  1. hello

Output

Hello , World !

If you received the Hello , World ! output, you’ve successfully made your Go program executable from both a specific and unspecified path on your server.

By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. You can find and subscribe to additional articles on installing and using Go within our “Go” tag