英文:
(from $GOROOT) ($GOPATH not set) in IntelliJ idea
问题
我已经开始在IntelliJ Idea中使用golang。
我有以下代码:
package main
import (
"fmt"
"github.com/zzz/stringutil"
)
func main() {
fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}
还有以下的stringutil.go文件:
// Package stringutil contains utility functions for working with strings.
package stringutil
// Reverse returns its argument string reversed rune-wise left to right.
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)
}
我收到以下错误:
src\github.com\zzz\hello\hello.go:5:2: cannot find package "src/github.com/zzz/stringutil" in any of:
C:\Go\src\src\github.com\zzz\stringutil (from $GOROOT)
($GOPATH not set)
我该如何通过IntelliJ配置环境变量,以便能够运行程序?
英文:
I've started using the golang in IntelliJ Idea.
I have the following code
package main
import (
"fmt"
"/github.com/zzz/stringutil"
)
func main() {
fmt.Printf(stringutil.Reverse("!oG ,olleH"))
}
and also I have the following stringutil.go file
// Package stringutil contains utility functions for working with strings.
package stringutil
// Reverse returns its argument string reversed rune-wise left to right.
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)
}
I'm receiving the following error:
src\github.com\zzz\hello\hello.go:5:2: cannot find package "src/github.com/zzz/stringutil" in any of:
C:\Go\src\src\github.com\zzz\stringutil (from $GOROOT)
($GOPATH not set)
How can I configure the env variables through Intellij so I can run the program?
答案1
得分: 7
这是解决该问题的快速修复方法。我正在使用IntelliJ IDEA作为我的编辑器。我正在使用MAC。
- 这是我的项目完整路径。你可以根据需要进行自定义。我的项目名称是adit /Users/mmdc/Documents/mataharimall-development/www/go/src/github.com/mataharimall/adit
英文:
This is the quick fix for this issue. I am using inttelliJ idea as my editor. I am using MAC
-
Go to preferences
-
Search for GO under language & frameworks
-
If your look for GOROOT it should be like screen shot below
-
If you look for GOPATH the setting should be like screen shoot below
- And this is my full path of the project. You can customize it to
follow your needs. My project name is adit /Users/mmdc/Documents/mataharimall-development/www/go/src/github.com/mataharimall/adit
答案2
得分: 1
首先,你需要删除“/github.com/zzz/stringutil
”开头的斜杠。它应该是“github.com/zzz/stringutil
”。
然后,你需要定义环境变量GOPATH并将其设置为某个可写的目录。
你可以参考这个指南在Windows上设置GOPATH:http://www.wadewegner.com/2014/12/easy-go-programming-setup-for-windows/
根据上述指南:
-
创建一个名为
C:\Projects\Go
的文件夹作为我的Go根工作空间。 -
创建GOPATH环境变量,并引用你的Go工作空间路径。要添加变量,点击“系统->高级系统设置->环境变量”...,然后在系统变量下点击“新建”。
-
将变量名称设置为
GOPATH
,值设置为你的Go工作空间路径(例如C:\Projects\Go
)。
英文:
First, you need to remove slash at start of "/github.com/zzz/stringutil
". It should be "github.com/zzz/stringutil
".
Then, you need to define environment variable GOPATH and set it to some writeable directory.
You can see this guide for setting up GOPATH on windows: http://www.wadewegner.com/2014/12/easy-go-programming-setup-for-windows/
From above:
-
create a
C:\Projects\Go
folder as my root Go workspace -
Create the GOPATH environment variable and reference your Go
workspace path. To add, clickSystem-> Advanced system settings-> Environment Variables
... and click New... under System variables -
Set the variable name to
GOPATH
and value to yourGo workspace path
(e.g.C:\Projects\Go
)
答案3
得分: 1
是的,请删除“/github.com/zzz/stringutil”中的斜杠,像这样“github.com/zzz/stringutil”,你可以使用Idea或VSCode,它们可以自动添加到导入中,新的Golanger。^_^'
英文:
Yes, remove the slash of "/github.com/zzz/stringutil", like this "github.com/zzz/stringutil",
you can use idea or vscode, it can auto add to imports, new golanger. ^_^'
答案4
得分: -1
如果您是通过Homebrew安装的,请执行以下操作:
brew info go
它会给出有关$GOROOT的指示。我的是:
您可能希望将基于GOROOT的安装位置添加到您的PATH中:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
此外,不要忘记加载您的bash配置文件!现在应该可以工作了。如果还不行,请尝试在IntelliJ Idea中执行File/Invalidate Caches/Restart...
。
英文:
If you installed via homebrew, just do:
brew info go
And it gives indications for your $GOROOT. Mine is:
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
Also, don't forget to source your bash profile! It should work now. If not, try to do File/ Invalidate Caches/ Restart...
in IntelliJ Idea.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论