文章目录
系统硬件与操作系统
Linux,Centos7
安装过程
1.安装Centos7虚拟机
用的是VirtualBox,下载Centos7的镜像安装就好。
值得注意的是,若想要安装图形化界面的话,则需要在安装过程中软件选择那里选择GNOME桌面。
2.安装 VSCode 编辑器
首先,为了下载速度更快更方便,可以先把Centos的源换成清华源。
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
再把/etc/yum.repos.d/CentOS-Base.repo文件里面的内容改为:
# 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
可以直接用vi来编辑文件。
最后执行如下指令即可:
sudo yum makecache
安装VSCode之前需要安装snapd,(已安装的可以忽略)
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
安装完snapd之后就能够安装VSCode了。
sudo snap install --classic code # or code-insiders
3.安装 golang
3.1安装
sudo yum install golang --nogpgcheck
–nogpgcheck用于跳过公钥检查,不然就会提示xxx.rpm公钥尚未安装。
3.2设置环境变量
通过vi编辑~/.profile文件,这个文件不存在(即显示为新文件)也没有关系。在文件中添加:
export GOPATH=$HOME/gowork
export PATH=$PATH:$GOPATH/bin
最后执行这些配置
source $HOME/.profile
3.3创建hello world
通过reboot指令重启虚拟机。
mkdir $GOPATH/src/github.com/github-user/hello -p
随后在该目录下创建hello.go。
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
最后在终端运行。
$ go run hello.go
hello, world
4.安装必要的工具和插件
4.1安装Git客户端
sudo yum install git --nogpgcheck
(注意提示xxx.rpm公钥尚未安装的话,同样也要加–nogpgcheck后缀)
4.2安装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
此时进入VSCode,按提示安装仍然failed,网上找了一些博客也是行不通,暂时就不管了。遇到了困难,就不做了,睡大觉。
但是在终端运行还是没问题的,VSCode暂时沦为彻底的“纯”文件编辑工具。
$ go install github.com/github-user/hello
$ hello
hello, world
5.安装与运行 go tour
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/net
go get -u github.com/Go-zh/tour
最终执行tour即可。
6.我的第一个包与测试
6.1编写一个stringutil库
mkdir $GOPATH/src/github.com/github-user/stringutil
接着,在该目录中创建名为reverse.go的文件:
// 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
修改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测试
创建文件,$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)
}
}
}
6.3远程包
$ go get github.com/golang/example/hello
$ $GOPATH/bin/hello
Hello, Go examples!
问题或要点小结
除了前面所说的通过–nogpgcheck跳过公钥检查来解决xxx.rpm公钥尚未安装以外,在安装tour的时候还需要事先在$GOPATH/src/golang.org/x/目录下安装好net工具包。同时,在这里分享一个虚拟机硬盘扩容方案,VirtualBox上Centos7磁盘扩容。
没有足够的硬盘空间,实验环境也是装不了的。