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?
Open settings ( Ctrl+Alt+S ) and navigate to Go | GOPATH. In the file browser, navigate to the directory that you want to associate with GOPATH. In the following example, we configured to use different GOPATH directories for different scopes. GoLand will use the Module GOPATH as it is the narrowest scope configured.
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go ) on Windows.
Right-click the parent folder of a project, and select New | Go File. Click the parent folder of a project, press Alt+Insert , and select Go File. Click the parent folder of a project, navigate to File | New | Go File.
Create the GOPATH variable and reference your newly-created Go work-space. Go back to your Control Panel and navigate to System and then Environmental Variables. Then under System Variables click on New. To check that your path has been set correctly, enter “echo %GOPATH%” on the command line.
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
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. ^_^'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With