Installation of Go language development environment under Ubuntu
surroundings
VMware 15.0
Ubuntu 18.04
Step one installation
Use the system package management tool to install, there may be some abnormalities here, just follow the system prompts.
sudo apt-get install golang
Test whether the installation is successful and check the version
go version
Step two create a 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)
- pkg directory contains package objects
- bin directory contains executable commands
Therefore, we need to create a workspace directory and set the corresponding GOPATH for it. The workspace can be placed anywhere, here is placed in the home directory.
kangze@kangze:/$ mkdir $HOME/kangze/gopath
Next, create three subdirectories src pkg bin in this directory
mkdir src pkg bin
Step three set environment variables
export GOPATH=$HOME/kangze/gopath
export PATH=$PATH:$GOPATH/bin
Perform this configuration:
source ~/.bashrc
Check the configuration
$ go env
As you can see, GOPATH has now been modified.
Create Hello world!
After creating a workspace and setting environment variables, follow the usual practice to learn a language, usually starting with Hello World. In order to complete this epoch-making work, we first create a source code directory.
$ mkdir $GOPATH/src/gihub.com/gitee-user/hello -p
Use vscode to create hello.go (vscode can be downloaded in the Ubuntu application center)
Create a hello.go file in the source code directory, and write the following code in it.
package main
import "fmt"
func main() {
fmt.Printf("Hello World!\n")
}
run:
$ go run hello.go
Install the necessary plugins
Install Git
sudo apt-get install git
Some tools for installing go
Reference link https://github.com/northbright/Notes/blob/master/Golang/china/get-golang-packages-on-golang-org-in-china.md .
Download source code to local
Since Golang.org cannot be accessed under the Chinese network environment, it is different from running
go get golang.org/x/tools/...
To get Golang packages.
Therefore, you can use the mirror on github to get the code. First create a subdirectory under the GOPATH directory
mkdir -p $GOPATH/src/golang.org/x/
Download source code
go get -d github.com/golang/tools
Move tools to the directory created in the first step
cp $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/ -rf
Installation kit
go install golang.org/x/tools/go/buildutil
Close vscode, reopen it, and install it as prompted.
Install and run hello world
Execute the following command
go install github.com/github-user/hello
This command will generate a binary file and install the binary file into the bin directory. Enter hello to run.
Install and run go tour
Refer to https://github.com/Go-zh/tour .
go get github.com/Go-zh/tour/gotour
gotour
The results are as follows:
My first package and test
To write a library and program to use it. First select the package path and create the package directory:
mkdir $GOPATH/src/github.com/github-user/stringutil
Next, create a file named reverse.go in this directory with the following content:
// 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)
}
Now use the go build command to test the compilation of the package:
$ go build github.com/github-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"))
}
by
go install github.com/github-user/hello
To install the stringutil package.
Run the new version of this program.
hello
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 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)
}
}
}
Then use go test to run the test
go test github.com/github-user/stringutil
Problem summary
At the beginning, when creating a workspace, one less path was written when configuring environment variables.
(错误)export GOPATH=$home/gopath
(正确)export GOPATH=$home/kangze/gopath
Cause subsequent operations to fail.