×
Community Blog Quick Guide to Setup Go Project on Alibaba Cloud

Quick Guide to Setup Go Project on Alibaba Cloud

This tutorial introduces the Go project and discusses how you can set it up on Alibaba Cloud.

By Alex, Alibaba Cloud Community Blog author.

This tutorial gives a walkthrough on how to make Go builds from the project's source, specifically for open-source projects. Distributed under the BSD-style license, the project is easy to build on an Alibaba Cloud ECS instances that are installed with Ubuntu 18.04. In most cases, there's an option of using pre-compiled binary packages which eliminates the need to install from the scratch. In fact, the Getting Started documentation on the Golang website is an easier alternative. However, learning how to build from source is an important skill that comes handy for developers.

Google developed Go to meet the needs of a simple but robust language, strengthened by libraries and tools for building applications to fulfill efficiency and reliability requirements. Building from source allows implementing better versions. It allows compiling in either of two ways, the GCC Go Compiler or the GCC Go compiler. The compilers may execute in eight different ways for various operating systems and architectures, including amd64, 386, arm (ARM), arm64, ppc64 (ppc64le), MIPS (mipsle), mips64 (mips64le) or s390x. Ubuntu 18.04 is one of the operating systems these compilers support, and the same is the subject for this tutorial.

Tutorial Requirements

The requirements for this tutorial include:

  • An Alibaba Cloud ECS instance running Ubuntu 18.04
  • A sudo non-root user to access the server
  • Git version control
  • About 1-2 GB system RAM

Getting Started

It is critical to note that the installation of wget and git tools is a prerequisite for installing Go. Run the following command to install these tools in case they aren't pre-installed.

sudo apt-get update
sudo apt-get -y upgrade
sudo apt install wget git

Next, install requisite packages on the Ubuntu operating system (OS) such as libraries and compilers. Use the build-essential method to install these dependencies on the system. It includes most of the packages for compiling tools such as GNU Compiler Collection (GCC), G++, and more.

Run the command below to install the packages.

sudo apt-get install build-essential

Now let's change to a working directory using the following command.

cd ~

Install Go

There are three ways to install Go based on the users' preferences.

1) Using Golang Installer

Google's Golang installer is easy to use. Download the source using the link shown below. Run the following commands to download the installer on the system.

wget -q https://storage.googleapis.com/golang/getgo/installer_linux

Run the command below to make it executable.

chmod +x installer_linux 

Next, execute the installer by running the following command.

./installer_linux

Executing the preceding command downloads the latest Go version. The following snippet shows the response to the above command.

Downloading Go version go1.12.4 to /root/.go
This may take a bit of time...
Downloaded!
Setting up GOPATH
GOPATH has been set up!

One more thing! Run source /root/.bash_profile to persist the new environment variables to your current session, or open a new shell prompt.

Run the command as prompted by the installer to persist the environment variables for Go.

source /root/.bash_profile

Now run the command below to check the installed go version.

go version

The following snippet shows the output of the preceding command.

go version go1.12.4 linux/amd64

Currently, the latest Go version is go1.12.4 as shown in the output above.

Now, execute a pre-compiled Hello World as shown below.

go get github.com/golang/example/hello
hello 
Hello, Go examples!

2) Apt Install Command

The apt install command fetches Go from the Ubuntu 18.04 repository. Run the command below to install Go from the repository.

sudo apt install golang

Check the installed Go version by executing the following command.

go version

The following snippet shows the output of the preceding command.

go version go1.12.4 linux/amd64

Set a GOPATH to meet the installation requirements by running the following commands.

echo 'export GOPATH=$root/go' >> ~/.bashrc 
echo 'export PATH=${PATH}:${GOPATH}/bin' >> ~/.bashrc 
source ~/.bashrc 

Again, try executing a pre-compiled Hello World as shown below.

go get github.com/golang/example/hello
hello 
Hello, Go examples!

3) Snap Install

The snap install is the most flexible installation method among the three methods listed in this article. Most prominently, it allows choosing the version to install with ease. Run the command below to check the available versions.

snap info go

The following snippet shows the output of the preceding command.

name:      go
summary:   Go programming language compiler, linker, stdlib
publisher: Michael Hudson-Doyle (mwhudson)
contact:   michael.hudson@ubuntu.com
license:   BSD-3-Clause
description: |
  This snap provides an assembler, compiler, linker, and compiled libraries
  for the Go programming language.
snap-id: Md1HBASHzP4i0bniScAjXGnOII9cEK6e
channels:
  stable:         1.12.2        2019-04-15 (3584) 92MB classic
  candidate:      ¡ü
  beta:           ¡ü
  edge:           devel-ba978f5 2019-05-01 (3688) 92MB classic
  1.12/stable:    1.12.2        2019-04-15 (3584) 92MB classic
  1.12/candidate: ¡ü
  1.12/beta:      ¡ü
  1.12/edge:      ¡ü
  1.11/stable:    1.11.7        2019-04-15 (3586) 82MB classic
  1.11/candidate: ¡ü
  1.11/beta:      ¡ü
  1.11/edge:      ¡ü
  1.10/stable:    1.10.8        2019-01-24 (3133) 58MB classic
  1.10/candidate: ¡ü
  1.10/beta:      ¡ü
  1.10/edge:      ¡ü
  1.9/stable:     1.9.7         2018-06-13 (2117) 58MB classic
  1.9/candidate:  ¡ü
  1.9/beta:       ¡ü
  1.9/edge:       ¡ü
  1.8/stable:     1.8.7         2018-02-07 (1407) 51MB classic
  1.8/candidate:  ¡ü
  1.8/beta:       ¡ü
  1.8/edge:       ¡ü
  1.7/stable:     1.7.6         2017-06-02  (324) 48MB classic
  1.7/candidate:  ¡ü
  1.7/beta:       ¡ü
  1.7/edge:       ¡ü
  1.6/stable:     1.6.4         2017-05-17  (122) 49MB classic
  1.6/candidate:  ¡ü
  1.6/beta:       ¡ü
  1.6/edge:       ¡ü 

Run the following command to choose an installation to implement with snap from the available options.

sudo snap install go šCclassic

The following snippet shows the output of the preceding command.

2019-05-01T19:51:08Z INFO Waiting for restart...
go 1.12.2 from Michael Hudson-Doyle (mwhudson) installed

Now set the GOPATH as shown below.

echo 'export GOPATH=$root/go' >> ~/.bashrc 
echo 'export PATH=${PATH}:${GOPATH}/bin' >> ~/.bashrc 
source ~/.bashrc 

Again, execute a pre-compiled Hello World as shown below.

go get github.com/golang/example/hello
hello 
Hello, Go examples!

One of the most interesting facts about the snap installation is that it allows choosing any version. Consider the snippet below as an example.

sudo snap install go --channel 1.9 --classic                                                                                                                                                            
go (1.9/stable) 1.9.7 from Michael Hudson-Doyle (mwhudson) installed
go version                                                                                                                                                          
go version go1.9.7 linux/amd64 

Verifying the Environment Variables

Run the command below to verify the installed environment variables.

go env

The following snippet shows the output of the preceding command.

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GOPROXY=""
GORACE=""
GOROOT="/root/.go"
GOTMPDIR=""
GOTOOLDIR="/root/.go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build753398445=/tmp/go-build -gno-record-gcc-switches"

Configuring the Environment

It is easy to customize the environment variables of a Go project. Let's have a look at how to go about it. After configuring the environment for two installations in the preceding section, try to do it manually at this step. Let's see the structure of the Go directory.

Run the following command below in the working directory.

ls
output 
go  installer_linux  modoboa-installer

cd go
ls

The following snippet shows the output of the preceding command.

bin  src

cd bin
ls

Consecutively, it results in the following output.

hello

Change back to the working directory by using the following command.

cd ~
nano ~/.profile

Include the following code in the file.

. . .
export GOPATH=$root/
export PATH=$PATH:/root/go/bin:$GOPATH/bin

Save the file and exit the editor.

Note: Choose any installation method that suits the requirements.

Once the installation of the Go version is complete, proceed to test the system.

Testing the Installation

Now let's test the installation works by writing a simple application to run on it. Create a simple "Hello Go!" app for this purpose and switch back to the working directory for the test purposes.

Since for this tutorial, the directory to work with is already selected, create a file for the "Hello Go!" app as shown below.

nano hello.go

Add the following code in the file.

package main

import "fmt"

func main() {
    fmt.Printf("Hello Go!\n")
}

Execute the following command to run the app.

go run hello.go
output
Hello Go!

Running the application reflects the phrase "Hello Go!" on the terminal. In case it doesn't reflect, revisit the code to see if there are any errors and run again. If the problem persists, repeat the entire process.

Upgrading or Downgrading Go Versions

To meet the varying business requirements, users may either want to change the Go version running on the system, update to the latest Go version or downgrade from an unstable version to one of the proven ones. For all such purposes, change the active Git branch and rebuild Go again.

Therefore, first, check the Go version using the following command.

go version
Output
go version go1.12.4 linux/amd64

Given that there is no direct way to uninstall Go, delete the Go directory. If installed with snap or apt, locate it in the /usr/local/go directory. Next, remove the bin in the PATH variable. Lastly, install a lower or higher Go version.

Conclusion

The tutorial helps to successfully install Go from its source. Installing Go provides a robust foundation to build Go applications on the Alibaba Cloud ECS instance running Ubuntu 18.04. For more details about all the processes, check How to Write Go Code. Testing all these solutions results in an engaging experience for Go developers working with Alibaba Cloud. Subscribe to Alibaba Cloud ECS today and get started with practical applications.

Don't have an Alibaba Cloud account? Sign up for an account and try over 40 products for free worth up to $1300. Get Started with Alibaba Cloud to learn more.

0 0 0
Share on

Alex

42 posts | 4 followers

You may also like

Alex

42 posts | 4 followers

Related Products