找不到包 "rsc.io/quote"
cannot find package "rsc.io/quote"
我正在按照教程 (https://golang.org/doc/tutorial/getting-started) 开始使用 Go,但我已经 运行 遇到了问题。当我运行下面的代码:
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
我在控制台中收到以下错误消息:
C:\Users\myname\Documents\Work\GO\hello>go run hello.go
hello.go:7:8: cannot find package "rsc.io/quote" in any of:
C:\Program Files\Go\src\rsc.io\quote (from $GOROOT)
C:\Users\myname\go\src\rsc.io\quote (from $GOPATH)
我猜这是我安装 Go 的方式/位置的问题,有人可以解释一下吗?
谢谢
具有模块支持的go
工具会自动下载并安装依赖项。但要让它工作,您必须初始化您的模块。
将源代码保存在 .go
文件和 运行 与 go run hello.go
中是不够的,必须存在 go.mod
文件。
要初始化您的模块,请按照教程中的说明进行操作:
go mod init hello
输出应该是:
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
从 go 1.16 开始,您还必须 运行
go mod tidy
这将自动下载 rsc.io/quote
包:
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
接下来运行宁
go run hello.go
将输出:
Don't communicate by sharing memory, share memory by communicating.
2021/6/3 go版本go1.16.4linux/amd64
root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: /workspace/go_workspace/hello/go.mod already exists
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod tidy
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
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
Don't communicate by sharing memory, share memory by communicating.
运行 命令提示符下的这个命令:
go mod tidy
之后执行你的代码:
go run file_name.go
将 file_name.go
替换为您的 go 文件示例:
go run hello.go
我正在按照教程 (https://golang.org/doc/tutorial/getting-started) 开始使用 Go,但我已经 运行 遇到了问题。当我运行下面的代码:
package main
import "fmt"
import "rsc.io/quote"
func main() {
fmt.Println(quote.Go())
}
我在控制台中收到以下错误消息:
C:\Users\myname\Documents\Work\GO\hello>go run hello.go
hello.go:7:8: cannot find package "rsc.io/quote" in any of:
C:\Program Files\Go\src\rsc.io\quote (from $GOROOT)
C:\Users\myname\go\src\rsc.io\quote (from $GOPATH)
我猜这是我安装 Go 的方式/位置的问题,有人可以解释一下吗?
谢谢
具有模块支持的go
工具会自动下载并安装依赖项。但要让它工作,您必须初始化您的模块。
将源代码保存在 .go
文件和 运行 与 go run hello.go
中是不够的,必须存在 go.mod
文件。
要初始化您的模块,请按照教程中的说明进行操作:
go mod init hello
输出应该是:
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
从 go 1.16 开始,您还必须 运行
go mod tidy
这将自动下载 rsc.io/quote
包:
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
接下来运行宁
go run hello.go
将输出:
Don't communicate by sharing memory, share memory by communicating.
2021/6/3 go版本go1.16.4linux/amd64
root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod init hello
go: /workspace/go_workspace/hello/go.mod already exists
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
hello.go:6:2: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
root@zqf-vm:/workspace/go_workspace/hello# go mod tidy
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
root@zqf-vm:/workspace/go_workspace/hello# go run hello.go
Don't communicate by sharing memory, share memory by communicating.
运行 命令提示符下的这个命令:
go mod tidy
之后执行你的代码:
go run file_name.go
将 file_name.go
替换为您的 go 文件示例:
go run hello.go