How to organize go package, module and main when not in the same (sub) folder

I am learning go, please excus eif this is too trivial. I have looked around, but have not found anything explaining this to my satisfaction. I have a package as addition to package main with function main. This package I wrote is in the own folder under $GOPATH/src and go build passes without any problems.

The main.go is in different folder (but not sub-folder) than above mentioned package. This is how folders look:

                 .. many .go files each with definition:
                    package a_package

go.mod in a_package folder looks like this:


go 1.17

The import in main.go looks like this:

import "a_package"

I run go build in $GOPATH/src/a_package, no messages I run go run main.go in $GOPATH/src/main and get:

go:10:5: package a_package is not in GOROOT (/usr/lib/go/src/a_package)

I would like to understand how properly create go.mod, imports, package when I write packages outside of main folder path. Thank you.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum