"package XXX is not in GOROOT" when building a Go project

157,074

Solution 1

A pretty dumb conclusion (mostly on my part) but my issue came from having done go mod init in each of the folders. after removing go.mod and go.dep from each of the folders I did go mod init in, I could build without issue (through terminal)

Also, my packages in GoLand were not being detected because I had Go Modules enabled in the Settings. I disabled it and GoLand was able to index the external packages and my own packages.

Solution 2

You may have GO111MODULE set "on", which will be on the go mod. Turning off the GO111MODULE may resolve this problem.

go env -w GO111MODULE=off

Solution 3

In newer versions (post 1.13) of Go, you don't need to set environment variables like GOPATH, GOBIN, etc.

You also need to have a go.mod file at the project root. This will make the directory a Go module. This is also where the .git/ is located. This means that only one go.mod is needed per repository. Inside the project root you could do a go mod init remote-repo.com/username/repository

I installed Go using Homebrew on macOS so GOROOT is /opt/homebrew/Cellar/go/1.17.5/libexec. This location contains the standard library and runtimes for Go.

test and run commands are run in the format go COMMAND package_path/xxx. Without specifying the package_path ./ and just running go COMMAND xxx, the compiler assumes that the module xxx is located in GOROOT, and throws error package xxx is not in GOROOT (path/to/GOROOT/src/xxx) because it doesn't exist.

This behavior is expected because the package we are working with is not part of the Go SDK, i.e., not in GOROOT. The package we are working with will either end up in the go workspace or in the current working directory. Running go install compiles and puts an executable binary in $GOBIN (a.k.a $GOPATH/bin - here $GOPATH is the Go workspace). Running go build from inside a package compiles and puts an execuatble in that directory.

You haven't listed the files inside the server/ package and which file has the main function, so I'll emulate 3 workflows of a calculator each demonstrating more complexity. The last workflow is similar to your directory structure.

Directory Structure

Version 1:

  • Getting started with packages

  • Basic functionality

calculatorv1
├── go.mod                      <- go mod init github.com/yourname/calculatorv1
└── basic/
    ├── add.go
    ├── add_test.go
    ├── main.go
    ├── multiply.go
    └── multiply_test.go

Version 2:

  • More functionality

  • Multiple packages

calculatorv2
├── go.mod                      <- go mod init github.com/yourname/calculatorv2
├── main.go
└── basic/
│   ├── add.go
│   ├── add_test.go
│   ├── multiply.go
│   └── multiply_test.go
└─── advanced/
     ├── square.go
     └── square_test.go

Version 3:

  • Even more functionality

  • Nested packages

calculatorv3
├── go.mod                      <- go mod init github.com/yourname/calculatorv3
├── main.go
└── basic/
│   ├── add.go
│   ├── add_test.go
│   ├── multiply.go
│   └── multiply_test.go
└─── advanced/
     ├── square.go
     ├── square_test.go
     └── scientific/
         ├── declog.go
         └── declog_test.go

Workflow

Note: Substitute xxx with basic, advanced, or advanced/scientific depending on the version you're working with.

  • Initialize Go module in the project directory (one of calculatorv1, calculatorv2, or calculatorv3) using go mod init

  • Run tests

    go test -v ./... (from the project root, recursively execute all test suites)

    OR

    go test -v ./xxx (from the project root, run the test suite in package "xxx")

    OR

    cd xxx/
    go test -v            # (from inside the package)
    
  • Compile and execute package

    go run ./... (from the project root, recursively run all .go files except tests)

    OR

    go run ./xxx (from the project root, run all .go files in "xxx" package except tests)

    OR

    cd xxx
    go run .              # (from inside the package)
    

    NOTE: Only files in the main package are executable, i.e., files having declaration package main. This means that go run ./xxx will only work with version1, and not versions 2 and 3. So instead for versions 2 and 3, run go run main.go


Code

Very easy to fill in incomplete bits

Version 1

add.go

package main

func addition(x int, y int) int {
    return x + y
}

add_test.go

package main

import "testing"

func TestAdd(t *testing.T) {

    t.Run("adding two positive numbers", func(t *testing.T) {
        sum := addition(2, 2)
        expected := 4
        
        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })
    
    t.Run("adding two negative numbers", func(t *testing.T) {
        sum := addition(-3, -4)
        expected := -7

        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })

    t.Run("adding one positive and one negative integer", func(t *testing.T) {
        sum := addition(1, -3)
        expected := -2

        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })
    
}

main.go

package main

import "fmt"

func main() {
    var num1 int = 1
    var num2 int = 2
    
    sum := addition(num1, num2)
    product := multiplication(num1, num2)

    fmt.Printf("The sum of %d and %d is %d\n", num1, num2, sum)
    fmt.Printf("The multiplication of %d and %d is %d\n", num1, num2, product)
}

Version 2

main.go

package main

import (
    "fmt"
    "github.com/yourname/calculatorv2/basic"
    "github.com/yourname/calculatorv2/advanced"
)

func main() {
    var num1 int = 1
    var num2 int = 2
    
    product := basic.Multiplication(num1, num2)
    square := advanced.Square(num2)

    fmt.Printf("The product of %d and %d is %d\n", num1, num2, product)
    fmt.Printf("The square of %d is %d\n", num2, square)
}

multiply.go

package basic

func Multiplication(x int, y int) int {
    return x * y
}

multiply_test.go

package basic

import "testing"

func TestMultiply(t *testing.T) {

    t.Run("multiplying two positive numbers", func(t *testing.T) {
        sum := Multiplication(2, 2)
        expected := 4
        
        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })
    
    t.Run("multiplying two negative numbers", func(t *testing.T) {
        sum := Multiplication(-3, -4)
        expected := 12

        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })

    t.Run("multiplying one positive and one negative integer", func(t *testing.T) {
        sum := Multiplication(1, -3)
        expected := -3

        if sum != expected {
            t.Errorf("Expected %d; but got %d", expected, sum)
        }
    })
    
}

square.go

package advanced

func Square(x int) int {
    return x * x
}

Version 3

main.go

package main

import (
    "fmt"
    "github.com/yourname/calculatorv3/basic"
    "github.com/yourname/calculatorv3/advanced"
    "github.com/yourname/calculatorv3/advanced/scientific"
)

func main() {
    var num1 int = 1
    var num2 int = 2
    var num3 float64 = 2
    
    product := basic.Multiplication(num1, num2)
    square := advanced.Square(num2)
    decimallog := scientific.DecimalLog(num3)

    fmt.Printf("The product of %d and %d is %d\n", num1, num2, product)
    fmt.Printf("The square of %d is %d\n", num2, square)
    fmt.Printf("The decimal log (base 10) of %f is %f\n", num3, decimallog)
}

square.go

package advanced

func Square(x int) int {
    return x * x
}

declog.go

package scientific

import "math"

func DecimalLog(x float64) float64 {
    return math.Log10(x)
}

declog_test.go

package scientific

import "testing"

func TestDecimalLog(t *testing.T) {

    t.Run("adding two positive numbers", func(t *testing.T) {
        sum := DecimalLog(100)
        expected := 2.0
        
        if sum != expected {
            t.Errorf("Expected %f; but got %f", expected, sum)
        }
    })
    
    t.Run("adding two negative numbers", func(t *testing.T) {
        sum := DecimalLog(10)
        expected := 1.0

        if sum != expected {
            t.Errorf("Expected %f; but got %f", expected, sum)
        }
    })
}

Solution 4

To anyone who does want modules to work with GoLand after they have stopped doing so, make sure 'Enable Go modules integration' is checked in the Preferences as such:

Enable Go Modules Intergration

Solution 5

So it looks like if you running go mod init 'xxx' the xxx is core name of your project. In there main packages complete name is 'xxx/main' so if you have your project root folder like this:

root -> go mod init xxx
 |- main.go -> package "main"
 |- tools
      |- helper.go -> package "tools"

and you want to import tools package from main.go you need to import this "xxx/tools"

Share:
157,074
Michael Shum
Author by

Michael Shum

Updated on January 24, 2022

Comments

  • Michael Shum
    Michael Shum over 2 years

    I have a weird issue that arose when I took a break from this project. Upon starting up Goland, I'm riddled with errors when trying to run my project.

    The specific error, when building one of my packages, is: start.go: package project/game is not in GOROOT (C:\Go\src\project\game)

    I have a folder structure as such under C:\Users\username

    go
    |-src
       |-project
            |-game
                |-entity
                     |-whatever.go
                |-game_stuff.go
            |-server
    

    and my env vars are as such:

    GOROOT=C:\Go 
    GOPATH=C:\Users\ketchup\go 
    

    for each of the modules (project/game/entity, project/game, project/server), I did a git mod init.

    When building, Goland will try to run this:

    C:\Go\bin\go.exe build -o C:\Users\ketchup\AppData\Local\Temp\___go_build_project_server.exe project/server
    

    and return the error.

    Can anyone help me with this issue? Kind of lost since Goland was working fine the last time I opened it. Also not even sure what direction to look at - I'm pretty new to Go and I'm not really sure what documentation to look at :\ Thank you everyone!

  • Gunslinger
    Gunslinger over 3 years
    This didn't work properly for my use case. Any suggestions? I feel like this is a really normal thing to have to encounter.
  • dantechguy
    dantechguy about 3 years
    Consider adding some examples and adding formatting to your answer to help clarify your answer :)
  • Diego Iturriaga
    Diego Iturriaga about 3 years
    On Intellij IDEA be sure to activate "Language & Frameworks / Go / Enable Go Modules Integrations", that worked for me! ty
  • Jan Válek
    Jan Válek about 3 years
    And there is requirement that tools/helper.go package must be named "tools" same as directory. Else you will have error.
  • NM666
    NM666 about 3 years
    May encounter this situation if start a project without IDEs
  • infiniteLearner
    infiniteLearner over 2 years
    Agreed. It works, for intellij ultimate and for Goland IDEs.
  • Brendan W
    Brendan W over 2 years
    This is such a thorough and helpful answer! Thank you! I would upvote twice if I could
  • Bouke Versteegh
    Bouke Versteegh over 2 years
    This was it for me. Was running go 1.15 on windows, upgraded to 1.18, and solved it. Thanks!
  • Bruce Adams
    Bruce Adams about 2 years
    I am encountering this error but what is so wrong with running "go mod init"?