Tal Avissar
Tal Avissar

Reputation: 10314

(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?

Upvotes: 8

Views: 9417

Answers (4)

Paul Razvan Berg
Paul Razvan Berg

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

Faris Rayhan
Faris Rayhan

Reputation: 4636

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

Upvotes: 8

codefreak
codefreak

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:

  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-> Advanced system settings-> 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)

Upvotes: 1

ohko
ohko

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

Related Questions