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

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

Leave a Comment