Install go language development environment under ubuntu

surroundings

VirtualBox
Ubuntu 18.04

Install golang

Use the system package management tool to install golang. The advantage of this is that even if it is not the latest version, it will not affect normal use.

$ sudo apt-get install golang

After the installation is complete, use the go version command to view the version

$ go version

Insert picture here

Create workspace

Create workspace

Go code must be placed in the workspace. It is actually a directory, which contains three subdirectories:

  • The src directory contains Go source files, which are organized into packages (each directory corresponds to a package),
  • The pkg directory contains package objects,
  • bin directory contains executable commands

Use the mkdir command to create a workspace

$ mkdir $HOME/go

Configure GOPATH environment variable

The GOPATH environment variable indicates the location of your workspace (note that this location cannot be the same as your go installation directory)

$ export GOPATH=$HOME/go

Add the bin subdirectory of this workspace to your PATH

$ export PATH=$PATH:$GOPATH/bin

Then perform configuration

$ source $HOME/.profile

After the configuration is successful, you can use go env to check the configuration

$ go env

Insert picture description here

Create package path

Create a folder in the workspace to store your source files

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

Create Hello World!

First select the package path, and then create the file in the corresponding folder

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

Create a hello.go file
hello.go in the hello directory

package main

import "fmt"

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

Then enter the hello directory to run the file

$ go run hello.go

Insert picture description here

Install necessary tools and plugins

Install git

$ sudo apt-get install git

Download source code

# 创建文件夹(该命令要加-p,因为文件目录还不存在)
$ mkdir -p $GOPATH/src/golang.org/x/
# 下载源码
$ go get -d github.com/golang/tools
# copy 
$ cp $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/ -rf

Installation kit

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

Install and run the hello program

$ go install github.com/user/hello

This command will create an executable binary file named hello, install it under the bin file of the workspace, and build a hello command.
Insert picture description here
Note: When you change your file, you must rebuild your program, otherwise it will be executable The binary is still the previous file, and the file execution result will not change

Install go tour

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

Insert picture description here

My first package and test

Your first library

Write a library of your own and let the hello program use it.
Select the package path and create a directory

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

Create reverse.go file in the directory

// 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)
}

Build the file with the go build command

$ go build github.com/user/stringutil

Modify the previous hello program and use the library just created

package main

import (
	"fmt"

	"github.com/user/stringutil"
)

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

Reinstall the hello file with go install

$ go install github.com/user/hello

Insert picture description here

test

Go has a lightweight testing framework, which consists of the go test command and the testing package.

You can write a test 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 tests for stringutil by creating the file $GOPATH/src/github.com/user/stringutil/reverse_test.go

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 go test to test

$ go test github.com/user/stringutil

Insert picture description here

Problem summary

1. Pay attention to the correctness of the path when creating the workspace and directory files. If the path does not exist when creating the file, add the -p parameter
. 2. Every time you modify the program, you must re-install the temporary instruction to update the temporary instruction. Otherwise still run the previous executable file

Guess you like

Origin blog.csdn.net/weixin_43980838/article/details/108694987