最近一门课程实验需要用到Go语言,久闻其名,为了减少以后配置的坑,特地记了一些笔记。不定期更新。
1. Go的下载与安装
在下载页选择合适的版本下载。这里我选择的是go1.15.6.linux-amd64.tar.gz稳定版。
-
在root权限下解压
sudo tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz
-
将
/usr/local/go/bin
添加到环境变量中。增加到$HOME/.profile
或者/etc/profile
(系统全局)。对于使用zsh等非bash环境的用户,可以修改.zshrc
,并source
export PATH=$PATH:/usr/local/go/bin
-
若安装成功,如下命令可以正常执行
$ go version go version go1.15.6 linux/amd64
2. Go语言初探
用于用惯了IDEA与PyCharm,因此我也选择了Jetbrains家的Goland IDE。直接使用Jetbrains Toolbox可以方便下载。这是收费的,学生党可以用学校的邮箱去申请。没有licence的话可以使用免费的VSCode来开发。
2.1 Hello World
在目录下新建hello
文件在,并创建hello.go
文件
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
在命令行输入如下命令即可运行。
go run hello.go
2.2 调用外部包的代码
在pkg.go.dev搜索quote
包, 并看其文档内容。之后将其加入到代码中
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
引用外部包后,需要有一个go.mod
文件列出包的版本信息
$ go mod init hello
go: creating new go.mod: module hello
之后运行代码即可。gorun
会自动定位包并下载默认的最新版本。
$ go: finding module for package rsc.io/quote
hello.go:4:8:
module rsc.io/quote: Get "https://proxy.golang.org/rsc.io/quote/@v/list": dial tcp 172.217.24.17:443: i/o timeout
显然,在国内访问被墙了。考虑更换代理
go env -w GOPROXY=https://goproxy.cn
然后发现
$ go run hello.go
go: finding module for package rsc.io/quote
go: writing stat cache: mkdir /usr/local/go/bin/pkg: permission denied
go: downloading rsc.io/quote v1.5.2
hello.go:4:8: mkdir /usr/local/go/bin/pkg: permission denied
没想到还有权限问题。
sudo chmod -R 777 /usr/local/go
这下终于成功了
$ go run hello.go
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
Don't communicate by sharing memory, share memory by communicating.
3. 创建 Go 模块
创建greetings
文件夹,并运行
$ go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings
可以看到生成了一个go.mod
,内容为
module example.com/greetings
go 1.15
写greetings.go
代码
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
Go语言中,:=
是在一行中声明并初始化变量的简写,通过符号右边来指定变量的值。等价于
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)
4.从另一个模块中调用模块
类似 2.Go 语言初探 中,在greetings的同级目录下建立hello目录,写hello.go
package main
import (
"fmt"
"example.com/greetings"
)
func main() {
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
}
并创建一个新模块
$ go mod init hello
go: creating new go.mod: module hello
之后编辑hello模块来使用未发布的greetings模块
-
修改hello目录下的
gomod
module hello go 1.15 replace example.com/greetings => ../greetings
-
运行
$ go build go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000
-
注意到
go.mod
中多了一行。这被称为 require directivemodule hello go 1.15 replace example.com/greetings => ../greetings require example.com/greetings v0.0.0-00010101000000-000000000000
-
成功构建
hello
,运行择优./hello Hi, Gladys. Welcome!