BrokenImport unable to load package

Hi Everyone,

I am just messing around trying understand creating and importing packages i am not entirely sure what is going on and why it is trying to load a custom package from GOROOT instead of my GOPATH:

backend@Colton MINGW64 ~/Documents/goworkspace/src/golang-book/chapter11
$ go run main.go
main.go:5:2: package golang-book/chapter11/math is not in GOROOT (C:\Program Files\Go\src\golang-book\chapter11\math)

backend@Colton MINGW64 ~/Documents/goworkspace/src/golang-book/chapter11
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=C:\Users\backend\Documents\goworkspace\bin
set GOCACHE=C:\Users\backend\AppData\Local\go-build
set GOENV=C:\Users\backend\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOMODCACHE=C:\Users\backend\Documents\goworkspace\pkg\mod
set GOOS=windows
set GOPATH=C:\Users\backend\Documents\goworkspace
set GOPROXY=,direct
set GOROOT=C:\Program Files\Go
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set GOMOD=C:\Users\backend\go.mod
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\backend\AppData\Local\Temp\go-

I have a file named math.go with a relative path of chapter11\math\math.go code looks like the following (though i doubt this is relevant to the error i am getting…):

package math

func Average(xs []float64) float64 {
	total := float64(0)
	for _, x := range xs {
		total += x
	return total / float64(len(xs))

This is how things work, first GOROOT is checked, then the GOPATH.

Relative pathes are never considered this way. There was a way in old go versions that used ./foo/bar, not sure if that is still allowed, it already produced warnings back then…

One way would be to recreate the structure within GOPATH, then everything should be found.

Better way would be to use modules proper and “namespace” things under a GitHub or GitLab repository.

1 Like

I see, what if i don’t want to use GitHub for packages and only create packages locally?

Also, since Go checks the GOROOT path first before the GOPATH, why is it when i run go install in the math folder it does not install in GOROOT?

I followed this example here:

So this is no longer the correct way of doing things? is there a way you can send me some digestible uncryptic documentation?

It seems as if the book is from 2012, that’s 9 to 10 years. 5 years ago when I started go, GOPATH was still state of the art, but got replaced by modules about 2 years later and has in the meantime become deprecated.

So yes, doing it as described in the book is not how we do it anymore.

And you can use any other domain like namespace for your modules, though the first segment has to be domain name like, with a “hostname” and a top level domain separated by dots.

Though GitHub and GitLab are “blessed” by the go compiler and it knows how to properly access them. At the end you can use any git hoster with some effort.

As long as you follow the naming rules you could indeed stay fully on the localhost though I’d strictly advise against that.

Also go install will not install into the GOROOT as that is considered the location of the standard library and immutable.

Only a go update should touch the GOROOT.

1 Like

Excellent thanks for the info!!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.