(从 $GOROOT)($GOPATH 未设置)在 IntelliJ idea 中

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

(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 (
	&quot;fmt&quot;
	&quot;/github.com/zzz/stringutil&quot;
)

func main() {
	fmt.Printf(stringutil.Reverse(&quot;!oG ,olleH&quot;))
}

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 &lt; 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 &quot;src/github.com/zzz/stringutil&quot; 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。

  1. 进入首选项。

  2. 在语言和框架下搜索GO。

  3. 如果你寻找GOROOT,设置应该如下所示的屏幕截图:

    (从 $GOROOT)($GOPATH 未设置)在 IntelliJ idea 中

  4. 如果你寻找GOPATH,设置应该如下所示的屏幕截图:

    (从 $GOROOT)($GOPATH 未设置)在 IntelliJ idea 中

  1. 这是我的项目完整路径。你可以根据需要进行自定义。我的项目名称是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

  1. Go to preferences

  2. Search for GO under language & frameworks

  3. If your look for GOROOT it should be like screen shot below

    (从 $GOROOT)($GOPATH 未设置)在 IntelliJ idea 中

  4. If you look for GOPATH the setting should be like screen shoot below

(从 $GOROOT)($GOPATH 未设置)在 IntelliJ idea 中

  1. 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/

根据上述指南:

  1. 创建一个名为C:\Projects\Go的文件夹作为我的Go根工作空间。

  2. 创建GOPATH环境变量,并引用你的Go工作空间路径。要添加变量,点击“系统->高级系统设置->环境变量”...,然后在系统变量下点击“新建”。

  3. 将变量名称设置为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:

  1. create a C:\Projects\Go folder as my root Go workspace

  2. Create the GOPATH environment variable and reference your Go
    workspace path. To add, click System-&gt; Advanced system settings-&gt; Environment Variables... and click New... under System variables

  3. Set the variable name to GOPATH and value to your Go 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.

huangapple
  • 本文由 发表于 2016年12月16日 13:27:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/41177788.html
匿名

发表评论

匿名网友

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

确定