> # wget -c https://dl.google.com/go/go1.16.6.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
> # vim /etc/profile
export PATH=$PATH:/usr/local/go/bin
> # source /etc/profile
配置 Goproxy 环境变量
Bash (Linux or macOS)
export GOPROXY=https://goproxy.io,direct
PowerShell (Windows)
$env:GOPROXY = "https://goproxy.io,direct"
使配置长久生效 (推荐)
上面的配置步骤只会当次终端内生效, 如何长久生效呢, 这样就不用每次都去配置环境变量了。
Mac/Linux
# 设置你的 bash 环境变量
echo "export GOPROXY=https://goproxy.io,direct" >> ~/.profile && source ~/.profile
# 如果你的终端是 zsh,使用以下命令
echo "export GOPROXY=https://goproxy.io,direct" >> ~/.zshrc && source ~/.zshrc
Windows
1. 右键 我的电脑 -> 属性 -> 高级系统设置 -> 环境变量
2. 在 "[你的用户名]的用户变量" 中点击 "新建" 按钮
3. 在 "变量名" 输入框并新增 "GOPROXY"
4. 在对应的 "变量值" 输入框中新增 "https://goproxy.io,direct"
5. 最后点击 "确定" 按钮保存设置
package main
import (
"fmt"
"github.com/robfig/cron"
)
func main() {
i := 0
c := cron.New()
spec := "*/5 * * * * ?"
c.AddFunc(spec, func() {
i++
fmt.Println("cron running:", i)
})
c.Start()
select {}
}
> # go run cron.go
cron.go:6:2: no required module provides package github.com/robfig/cron; to add it:
go get github.com/robfig/cron
解决方法:
> # go get github.com/robfig/cron
go: downloading github.com/robfig/cron v1.2.0
go get: added github.com/robfig/cron v1.2.0
> # go run cron.go
cron running: 1
cron running: 2
cron running: 3
> # go.mod
module test
go 1.16
require github.com/robfig/cron v1.2.0 // indirect
我们去掉 require github.com/robfig/cron v1.2.0 // indirect 这一行的内容
问题再次出现
> # go run cron.go
cron.go:6:2: no required module provides package github.com/robfig/cron; to add it:
go get github.com/robfig/cron
> # go get github.com/robfig/cron
go get: added github.com/robfig/cron v1.2.0
> # go run cron.go
cron running: 1
cron running: 2
cron running: 3
第二种方式: 手工添加
> # vim go.mod
module test
go 1.16
require (
github.com/robfig/cron
)
> # go mod tidy
go: errors parsing go.mod:
D:\test\go.mod:7:2: usage: require module/path v1.2.3
我们发现需要添加版本号
> # vim go.mod
module test
go 1.16
require (
github.com/robfig/cron v1
)
注意: 版本号需要自己去github.com去查询
> # go mod tidy
> # cat go.mod
module test
go 1.16
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/fxamacker/cbor/v2 v2.3.0
github.com/minio/minio-go/v7 v7.0.11
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/robfig/cron v1.2.0
github.com/scylladb/termtables v1.0.0
go.uber.org/zap v1.17.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
我们发现自动添加了 v1.2.0 版本号
> # go run cron.go
cron running: 1
cron running: 2
cron running: 3
本文暂时没有评论,来添加一个吧(●'◡'●)