Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang package is not in GOROOT (/usr/local/go/src/packageName) [closed]

Tags:

module

go

Hi could someone help me run my main.go: go run main.go ?

There are two folders, which are next to each other:

proj1 folder has main.go, go.mod

package1 folder has package1.go, go.mod, utility.go

inside of main.go:

package main

import (
    "package1"
    "fmt"
)

func main() {
    y := package1.Struct1{
        v: "1",
    }
    z := package1.isTrue()
    fmt.Println(z)
}

inside my package folder: package1.go

package package1

type Package1 struct {}

func (a *Package1) IsTrue() bool {
    return true
}

My Go version: go1.15.2 linux/amd64

My Go env settings:

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user1/.cache/go-build"
GOENV="/home/user1/.config/go/env"
GOMODCACHE="/mnt/sda5/gopath/pkg/mod"
GOOS="linux"
GOPATH="/mnt/sda5/gopath"
GOROOT="/usr/local/go"
...

I tried : go install, go build ... results no error inside the package folder

go mod vendor, go run main.go, go get -u package1 ... result in the same message when run inside the proj1 folder:

package package1 is not in GOROOT (/usr/local/go/src/package1)

The VS Code Go Plugin Linter shows no problem. Please help. Thank you!

like image 657
Russo Avatar asked Oct 20 '20 15:10

Russo


2 Answers

To solve the error i was facing package package1 is not in GOROOT (/usr/local/go/src/package1)

I had to ensure the environment variables were correctly configured.

I added those lines in the bashrc file:

export GO111MODULE=on
#GOPATH MUST BE OUTSIDE OF GOROOT directory!!!
export GOPATH=/mnt/sda1/programming/gopath
export PATH=$PATH:$GOPATH/bin

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

I loaded the bashrc file in the terminal:

source ~/.bashrc

Now i can execute following procedure to program with the Go language.

Make a new main folder... Inside this main folder: make main.go file begin with package main

Run the command below:

go mod init main

make another folder with the new package name: e.g. package1

inside the package1 folder: make all files with package package1 in its 1st line ... but DO NOT MAKE MOD FILE inside this package folder!!!

in your main.go, you can import that package and use it

import "main/package1"
y := package1.Struct1{a: 1, b: 2,...}
z := y.func1()
like image 83
Russo Avatar answered Nov 12 '22 20:11

Russo


This page is the top search result for...

package xxxxx is not in GOROOT (/usr/lib/go/src/xxxxx)

In my case, I was trying:

go build mything

That gave me the error. The solution was simple, I needed to be more specific:

go build mything.go

like image 27
PJ Brunet Avatar answered Nov 12 '22 20:11

PJ Brunet