ubuntu18.04 install go language development environment

One, install golang

First check
Insert picture description here
if there is a version of go on the system. If there is no version of go, install through the system package to
Insert picture description here
check whether the installation is successful
Insert picture description here

(It will be used later on vscode, you can also install sudo apt-get install code through the system library, so I won’t repeat it here)

Two, create a working directory

Create a new directory gopath (the name can be any) under the home directory, and create three subdirectories under this directory:

  • src-Each subdirectory in it is a package. Inside the package is the Go source code file

  • pkg-the object file of the package generated after compilation

  • bin-the generated executable file.

Insert picture description here

Three, set environment variables

Edit the .bashrc file in the home directory and add the following content at the end:
(home/xumy/ is my home directory)
Insert picture description here
Run source ~/.bashrcto make the above configuration effective.

Run go envto check whether the above configuration takes effect.
Insert picture description here

Fourth, write the first go language program hello.go

(Refer to https://go-zh.org/doc/code.html )

The source file should be placed in the src folder of the working directory, and you can create your own package path below. Here, take github.com/github-user/hello as an example. This is equivalent to the project file in C++, all related to the project The code can be placed here.

Create the package path:

mkdir $GOPATH/src/github.com/github-user/hello -p

Create a file named hello.go in this directory, the content of the file is as follows:

package main
import "fmt"

func main() {
    
    
	fmt.Printf("hello, world\n")
}

Five, install some go tools

Open the .go file in vscode, it will prompt you to install some extensions, but it will fail after clicking install. The reason for the failure is that the golang.org website is walled, which leads us to pull the golang.org/x/tools directory The content failed.
Insert picture description here
Manual installation
(refer to https://github.com/northbright/Notes/blob/master/Golang/china/get-golang-packages-on-golang-org-in-china.md )

First create a subdirectory under the $GOPATH directory:

mkdir -p $GOPATH/src/golang.org/x/

The function of the parameter -p is to create a directory when the path does not exist.

Then download the source code of the tools directory of the github mirror

go get -d github.com/golang/tools

Then move the tools to the directory created in the first step

cp $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/ -rf

Finally, install the toolkit

go install golang.org/x/tools/go/buildutil

Finally, close vscode and reopen it. Follow the prompts to install and you will see the result:
Insert picture description here
You’re done, ready to go!

Six, run the first program

carried out go install github.com/github-user/hello

This command will build the hello command and produce an executable binary file. Then it will install the binary file as hello (hello.exe under Windows) into the bin directory of the workspace. In our example, it is $GOPATH/bin/hello, specifically $HOME/go/bin/hello.

Note that this command can be executed in any path. The go tool will search for the source code in the github.com/user/hello package according to the workspace specified by GOPATH.

Now, we can enter its full path on the command line to run it:
$GOPATH/bin/hello
if $GOPATH/bin has been added to the PATH, just enter the binary file name:
hello

Insert picture description here

At this point, the go installation process is over. The following are some extensions.

Seven, install and run tour

( Detailed stamp https://github.com/Go-zh/tour )
Because the github download is too slow, you can only install gotour in the tour directory. The results are as follows:
Insert picture description here

8. My first library and test

(Detailed stamp https://go-zh.org/doc/code.html )

  • aims

We have to write a library and let hello.go use the methods defined in this library.

  • Creation of library directory and writing of library content

Similar to writing a program, the first step is to create a package path for the library.
mkdir $GOPATH/src/github.com/github-user/stringutil

Create a new file reverse.go in this directory, the content of the file is as follows:

// stringutil 包含有用于处理字符串的工具函数
package stringutil

//Reverse 将其实参字符串左右反转
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)
}

Use the go build command to test the compilation of the package:

go build github.com/user/stringutil

After confirming that the stringutil package is built, modify the original hello.go file (it is located at $GOPATH/src/github.com/github-user/hello) to use it:

package main
import  (
	"fmt"
	"github.com/github-user/stringutil"
)

func main() {
    
    
	fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}

Finally, you can run after reinstalling the hello program (go will automatically install all the things that the hello program depends on, including stringutil)
Insert picture description here

  • test

Go has a lightweight testing framework. You can write tests by creating a file whose name ends in _test.go and contains functions named TestXXX and signed as func (t *testing.T). The test framework will run each of these functions; if the function calls a function that indicates failure such as t.Error or t.Fail, the test indicates failure.

We can add a test for stringutil by creating the file $GOPATH/src/github.com/github-user/stringutil/reverse_test.go, the content is as follows:

package stringutil

import "testing"

func TestReverse(t *testing.T) {
    
    
	cases := []struct {
    
    
		in, want string
	} {
    
    
		{
    
    "Hello, world", "dlrow ,olleH"},
		{
    
    "Hello, 世界", "界世 ,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)
		}
	}
}

Use the go test github.com/github-user/stringutilcommand to run the test
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43867940/article/details/108558661