Reputation: 10314
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?
Upvotes: 8
Views: 9417
Reputation: 21470
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.
Upvotes: -1
Reputation: 4636
This is the quick fix for this issue. I am using inttelliJ idea as my editor. I am using MAC
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
Upvotes: 8
Reputation: 7141
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, click System-> Advanced system settings-> Environment Variables
... and click New... under System variables
GOPATH
and value to your Go workspace path
(e.g. C:\Projects\Go
)Upvotes: 1
Reputation: 31
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. ^_^'
Upvotes: 1