Service computing (1)-install go language development environment


System hardware and operating system

Linux,Centos7

Installation process

1. Install Centos7 virtual machine

VirtualBox is used, just download the image of Centos7 and install it.

Centos7 mirror download

Centos7 virtual machine installation

Insert picture description here

It is worth noting that if you want to install a graphical interface, you need to select the GNOME desktop where the software is selected during the installation process.

2. Install VSCode editor

First of all, in order to download faster and more convenient, you can first change the Centos source to Tsinghua source.

sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

Then change the contents of the /etc/yum.repos.d/CentOS-Base.repo file to:

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#


[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7



#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7



#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

You can use vi directly to edit files.

Finally, execute the following command:

sudo yum makecache

You need to install snapd before installing VSCode, (installed can be ignored)

sudo yum makecache

sudo yum install yum-plugin-copr

sudo yum copr enable ngompa/snapcore-el7

sudo yum -y install snapd

sudo systemctl enable --now snapd.socket

sudo ln -s /var/lib/snapd/snap /snap

After installing snapd, you can install VSCode.

sudo snap install --classic code # or code-insiders

3. Install golang

3.1 Installation

sudo yum install golang --nogpgcheck

-Nogpgcheck is used to skip the public key check, otherwise it will prompt that the xxx.rpm public key has not been installed.

3.2 Set environment variables

Edit the ~/.profile file through vi, and it does not matter if this file does not exist (that is, it is displayed as a new file). Add in the file:

export GOPATH=$HOME/gowork

export PATH=$PATH:$GOPATH/bin

Finally perform these configurations

source $HOME/.profile

3.3 create hello world

Restart the virtual machine through the reboot command.

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

Then create hello.go in this directory.

package main

import "fmt"

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

Finally, run it in the terminal.

$ go run hello.go
hello, world

4. Install the necessary tools and plugins

4.1 install Git client

sudo yum install git --nogpgcheck

(Note that if the xxx.rpm public key has not been installed, the suffix –nogpgcheck should also be added)

4.2 Some tools for installing go

mkdir $GOPATH/src/golang.org/

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

go get -d github.com/golang/tools

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

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

At this time, I entered VSCode and installed according to the prompts. It still failed. I found some blogs on the Internet and it didn’t work. If you encounter difficulties, don't do it and get a good night's sleep .

But running in the terminal is still no problem, VSCode temporarily reduced to a complete "pure" file editing tool .

$ go install github.com/github-user/hello
$ hello
hello, world

5. Install and run go tour

cd $GOPATH/src/golang.org/x/

git clone https://github.com/golang/net

go get -u github.com/Go-zh/tour

Finally, execute the tour.

Insert picture description here
Insert picture description here

6. My first package and test

6.1 Write a stringutil library

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

Next, create a file named reverse.go in this 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)
}

go build github.com/github-user/stringutil

Modify hello.go:

package main

import (
	"fmt"

	"github.com/github-user/stringutil"
)

func main() {
	fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}
$ go install github.com/github-user/hello
$ hello
Hello, Go!

6.2 Test

Create a 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)
		}
	}
}

Insert picture description here

6.3 Remote package

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

Question or point summary

In addition to the aforementioned problem of skipping the public key check by -nogpgcheck to solve the problem that the xxx.rpm public key has not been installed, you also need to install the net tool in the $GOPATH/src/golang.org/x/ directory before installing the tour. package. At the same time, share a virtual machine hard disk expansion solution here, Centos7 disk expansion on VirtualBox .

Insert picture description here

Without enough hard disk space, the experimental environment cannot be installed.

Reference blog link

Install Snapd in CentOS 7

When using the yum installer, it prompts that the xxx.rpm public key has not been installed

Centos7 disk expansion on VirtualBox

Guess you like

Origin blog.csdn.net/qq_43278234/article/details/108566781