包不在 GOROOT 中。

huangapple go评论92阅读模式
英文:

Package is not in GOROOT

问题

main.go:5:2: 包greetings不在GOROOT中(C:\Program Files\Go\src\greetings)

我正在阅读《Head First Go》,书中写道要在c:/users/username/go/src/packagename和c:/users/username/go/src/project_name中创建文件夹。

当我尝试从project_name文件夹中运行go run main.go时,出现了以下错误:
main.go:5:2: 包greetings不在GOROOT中(C:\Program Files\Go\src\greetings)

我在C盘上不想保存代码。我想从D盘运行代码,这可能吗?我该如何做到?

英文:

main.go:5:2: package greetings is not in GOROOT (C:\Program Files\Go\src\greetings)

I am reading Head first go and it was written to create folder in c:/users/username/go/src/packagename and c:/users/username/go/src/project_name

when i try to run

go run main.go from project_name folder, i got the following error.
main.go:5:2: package greetings is not in GOROOT (C:\Program Files\Go\src\greetings)

C:\Users\agriz>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\agriz\AppData\Local\go-build
set GOENV=C:\Users\agriz\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\agriz\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\agriz\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\agriz\AppData\Local\Temp\go-build2047492330=/tmp/go-build -gno-record-gcc-switches

I dont want to keep the code in c drive. I want to run codes from D:/ is that possible? How can i do that?

答案1

得分: 16

以下是翻译好的内容:

set GO111MODULE=offset GOPATH 到所需的位置也可以正常工作。

go env -w GO111MODULE=off

set GOPATH=D:\go

这个方法解决了我的问题。

英文:

set GO111MODULE=off and set GOPATH to desired location is also working good.

go env -w GO111MODULE=off

and

set GOPATH=D:\go

This one fixed my problems.

答案2

得分: 6

你可以使用go mod,并明确给出要初始化的模块的路径。

根据文档:

Init会初始化并在当前目录下写入一个新的go.mod文件,实际上是在当前目录下创建一个新的模块。go.mod文件不能已经存在。如果可能的话,init会从导入注释(参见'go help importpath')或版本控制配置中猜测模块路径。如果要覆盖这个猜测,可以提供模块路径作为参数。

示例:

   'go mod init example.com/m' 用于初始化一个v0或v1模块
   'go mod init example.com/m/v2' 用于初始化一个v2模块

关于如何设置你的Go程序的更多详细信息可以在这里找到。

英文:

You can use go mod and explicitly give the path to the module you want to initialize.

From Documentation

> Init initializes and writes a new go.mod to the current directory, in
> effect creating a new module rooted at the current directory. The file
> go.mod must not already exist. If possible, init will guess the module
> path from import comments (see 'go help importpath') or from version
> control configuration. To override this guess, supply the module path
> as an argument.

Example

   'go mod init example.com/m' to initialize a v0 or v1 module
   'go mod init example.com/m/v2' to initialize a v2 module

Further details on how to setup your go-program can be found here.

huangapple
  • 本文由 发表于 2021年8月7日 22:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68693154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定