Service Computing 02 - Installing go Language Development Environment

Install golang

  • Use terminal command to install golang

    $ sudo yum install golang
    
  • You can view the installed version and installation directory

    $ rpm -ql golang |more
    
    $ go version
    

You can see that the golang language has been installed in the following directory, version 11.5

Configuring environment variables

  1. Create a workspace

    $ mkdir $HOME/gowork
    
  2. Add in ~/. profile file

    export GOPATH=$HOME/gowork
    export PATH=$PATH:$GOPATH/bin
    

    Then execute $source $HOME/.profile to make the configured environment take effect

  3. go env check configuration

    GOPATH and GOROOT were added successfully

Run Hello world

  • After the installation and configuration is successful, we can try to write a go language applet.
  1. You can enter $go at the terminal to learn how to execute a series of go commands:

  2. Create the appropriate package directory in the workspace

  3. Create a hello.go file under this directory and write a program that outputs "hello world":

    package main
    
    import "fmt"
    
    func main() {
      fmt.Printf("hello, world\n")
    }
    
  4. Build and install this program with go tools

    $ go install github.com/user/hello
    
  5. In this way, we can run the generated binary files directly at the terminal or go run hello.go.

  6. Then we can initialize the warehouse and submit the first file.

    $ cd $GOPATH/src/github.com/user/hello
    $ git init
    $ git add hello.go
    $ git commit -m "initial commit"
    

Establishing libraries and testing

  • Write a library and let the hello program use it

  • Create a package directory in the following path:

    $ mkdir $GOPATH/src/github.com/user/stringutil
    

    Create a reverse.go file in this directory, which reads as follows:

    package stringutil
    
    func Reverse(s string) string {
    	r := []rune(s)
    	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
    		r[i], r[j] = r[j], r[i]
    	}
    	return string(r)
    }
    

    You can test the program with $go build

  • Modify the hello.go file and call the stringutil package in the file:

    package main
    
    import (
    	"fmt"
    
    	"github.com/user/stringutil"
    )
    
    func main() {
    	fmt.Printf(stringutil.Reverse("!oG ,olleH"))
    }
    

    Then, when we execute the hello file, we can see the output of a reverse string:

    • Go can automatically retrieve packages from the remote code base, that is, $go get instructions for remote import
    • $go get can check out code packages from Mercurial, Git, Subversion, Bazaar and other systems
    • Go has a lightweight test framework consisting of the $go test command and the testing package
      We can write a test file for stringutil as follows:
    package stringutil
    
    import "testing"
    
    func TestReverse(t *testing.T) {
    	cases := []struct {
    		in, want string
    	}{
    		{"Hello, world", "dlrow ,olleH"},
    		{"Hello, world", "World Age ,olleH"},
    		{"", ""},
    	}
    	for _, c := range cases {
    		got := Reverse(c.in)
    		if got != c.want {
    			t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
    		}
    	}
    
  • go test runs the test

Install the necessary tools and plug-ins

Install git client

  • Install the git client using the following instructions:

    sudo yum install git
    


You can see that we have installed the latest version of the git client

Install VSCode Editor

To facilitate programming in CentOS, we can install VSCode, a lightweight editor.

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" 

Installation with yum instructions

yum check-update
sudo yum install code

After successful installation, open the interface as follows:

Install some go tools

After entering the VSCode homepage, we will be prompted to install some go language tools.
But because of https://golang.org/wall, we can't install it officially.
So we create a directory locally and go to github to download:

mkdir $GOPATH/src/golang.org/x/
go get -d github.com/golang/tools

Discovered unable to download source code

We can go into the catalog and clone down.

cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/tools.git

Final installation

go install golang.org/x/tools/cmd/goimports

More about golang

Install gotour

go tour is a browser-based interactive go programming guide built by Google, which can effectively help us learn go language.
Install and run go tour using the following instructions

	$ go get github.com/Go-zh/tour/gotour
	$ gotour

go language programming exercises

  • So far, my go language development environment has been almost installed.
    Now we can use the existing tools and learning resources to complete some programming exercises.
    For example, a fast sorting algorithm is implemented in go language.

    Program Link: http://139.9.57.167:20080/share/blv3b5ed0lit3phfpi50?secret=false

Keywords: git github Go sudo

Added by Elarion on Sun, 15 Sep 2019 16:11:56 +0300