Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(from $GOROOT) ($GOPATH not set) in IntelliJ idea

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?

like image 772
Tal Avissar Avatar asked Dec 16 '16 05:12

Tal Avissar


People also ask

How do I set up a Gopath?

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.

Where is $Gopath?

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.

How do I import a Golang project into IntelliJ?

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.

How do I change the Gopath in Windows?

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.


2 Answers

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

    enter image description here

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

enter image description here

  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
like image 56
Faris Rayhan Avatar answered Sep 29 '22 09:09

Faris Rayhan


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. ^_^'

like image 31
ohko Avatar answered Sep 29 '22 09:09

ohko