Why Does $GOPATH/src Not Exist?

122 views
Skip to first unread message

Björn Försterling

unread,
Feb 22, 2025, 7:52:53 PMFeb 22
to golang-nuts
Hello,

according to "The Go Programming Lanuage" (Alan A. A. Donovan/Brian W. Kernighan)
page 291 the "GOPATH" should have a subdirectory named "src".

However that dir is not present on my system:

$ ls $(go env GOPATH)
bin  pkg

When doing a type check with the "go/types" package and the default importer (importer.Default()) it will also search the imported package inside GOPATH/src:

could not import github.com/Code-Hex/Neo-cowsay/v2 (can't find import: "github.com/Code-Hex/Neo-cowsay/v2": cannot find package "github.com/Code-Hex/Neo-cowsay/v2" in any of:
/usr/lib/go/src/github.com/Code-Hex/Neo-cowsay/v2 (from $GOROOT)
/home/bf/go/src/github.com/Code-Hex/Neo-cowsay/v2 (from $GOPATH))

I installed the external package with "go get ..." and I am able to build the
module that uses the external package.

Does anyone know how it can happen that $GOPATH/src does not exist?

Regards
Bjoern

Kurtis Rader

unread,
Feb 22, 2025, 10:13:44 PMFeb 22
to Björn Försterling, golang-nuts
That book was first published in 2015 -- ten years ago. Things have changed a lot since then. In particular, GOPATH mode is no longer the default and effectively a historical artifact having been replaced by Go module mode. In fact, Go modules were first introduced in Go 1.11 which was released in 2018 -- three years after that book was first published. When reading "how to" information, whether in a book or a blog post, you have to consider when the advice was published and whether it is old enough to still be relevant.

I've never watched any videos by this person before but this video seems like a thoughtful, relatively recent, review of the book: https://www.youtube.com/watch?v=79R27PMKcY0. In particular at the 5:50 minute mark they point out that the book doesn't talk about Go modules and GOPATH is no longer relevant.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/96b1e8c0-a280-4f55-815d-acff4985fb96n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Björn Försterling

unread,
Feb 23, 2025, 2:55:24 AMFeb 23
to Kurtis Rader, golang-nuts
Thanks for the insights.

I still need to figure out how to tell the `Config` method of the `go/types` package to look for imports at the right places. (which is probably at `$(go env GOMODCACHE)`)

peterGo

unread,
Feb 23, 2025, 9:59:13 AMFeb 23
to golang-nuts
On Saturday, February 22, 2025 at 7:52:53 PM UTC-5 Björn Försterling wrote:
Hello,

according to "The Go Programming Lanuage" (Alan A. A. Donovan/Brian W. Kernighan)
page 291 the "GOPATH" should have a subdirectory named "src".

However that dir is not present on my system:


The Go programming language is covered by a compatibility guarantee.
 
Go 1 and the Future of Go Programs    
https://go.dev/doc/go1compat    

The Go programming tools are not covered by a compatibility guarantee.

Go Release History    
https://go.dev/doc/devel/release  

Because of the compatibility guarantee, The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan, published Oct 26, 2015, is still valid for the programming language. It is often no longer valid for the programming tools.    

The Go Programming Language      
https://www.gopl.io/        

Start with the official, authoritative Go documentation, especially, Developing modules.

Go Documentation    
https://go.dev/    
https://go.dev/learn/    
https://go.dev/doc/      
 
Some YouTube video book review, found using Google Search, is unlikely to be helpful.

peter

peterGo

unread,
Feb 23, 2025, 10:09:51 AMFeb 23
to golang-nuts
Go Wiki: GOPATH   

peter
Reply all
Reply to author
Forward
0 new messages