Go get: where did it go? v1.18

I’m reading Mastering Go, Second Edition, by Mihalis Tsoukalos
I understand this was written for v1.12, and things have changed since, in relation to go modules, so that many commands do not work as such anymore.

I am in chapter 6, running examples from the book in ~/go/src/Mastering-Go-Second-Edition/ch06 (my bash prompt: PS1='\W> 'shows the short directory name).

ch06> go get github.com/mattn/go-sqlite3
go: go.mod file not found in current directory or any parent directory.
	'go get' is no longer supported outside a module.
	To build and install a command, use 'go install' with a version,
	like 'go install example.com/cmd@latest'
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.
ch06> go install github.com/mattn/go-sqlite3@latest
package github.com/mattn/go-sqlite3 is not a main package
ch06> mkdir /tmp/go-sqlite3
ch06> cd /tmp/go-sqlite3
go-sqlite3> go mod init go-sqlite3
go: creating new go.mod: module go-sqlite3
go-sqlite3> go get github.com/mattn/go-sqlite3
go: added github.com/mattn/go-sqlite3 v1.14.14
go-sqlite3> go get github.com/mattn/go-sqlite3@latest
go-sqlite3> find ~/go -type d -name go-sqlite3
/home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3
go-sqlite3> cd -
/home/marc/go/src/Mastering-Go-Second-Edition/ch06
ch06> go run htmlT.go
htmlT.go:6:2: no required module provides package github.com/mattn/go-sqlite3: go.mod file not found in current directory or any parent directory; see 'go help modules'
ch06> go build htmlT.go
htmlT.go:6:2: no required module provides package github.com/mattn/go-sqlite3: go.mod file not found in current directory or any parent directory; see 'go help modules'
ch06> ll -d $(find ~/go -type d -name go-sqlite3)
drwxrwxr-x 3 marc marc 4096 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3
ch06> ll $(find $(find ~/go -type d -name go-sqlite3) -type f)
-rw-rw-r-- 1 marc marc       9 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/list
-rw-rw-r-- 1 marc marc      52 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/v1.14.14.info
-rw-rw-r-- 1 marc marc       0 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/v1.14.14.lock
-rw-rw-r-- 1 marc marc      44 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/v1.14.14.mod
-rw------- 1 marc marc 2521345 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/v1.14.14.zip
-rw-rw-r-- 1 marc marc      47 Jul 15 15:20 /home/marc/go/pkg/mod/cache/download/github.com/mattn/go-sqlite3/@v/v1.14.14.ziphash

I would have expected the timestamp to show today’s time, not 3 days ago… Where was go-sqlite3 installed?

Thanks

Answering my own question…

ch06> ll -d ~/go/pkg/mod/github.com/mattn/go-sqlite3@v1.14.14/
dr-xr-xr-x 4 marc marc 4096 Jul 15 15:20 /home/marc/go/pkg/mod/github.com/mattn/go-sqlite3@v1.14.14/
ch06> go mod init htmlT
go: creating new go.mod: module htmlT
go: to add module requirements and sums:
	go mod tidy
ch06> go mod tidy
go: finding module for package github.com/mactsouk/myModule/v2
go: finding module for package github.com/mattn/go-sqlite3
go: finding module for package github.com/mactsouk/myModule
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.14
go: found github.com/mactsouk/myModule in github.com/mactsouk/myModule v1.1.0
go: found github.com/mactsouk/myModule/v2 in github.com/mactsouk/myModule/v2 v2.1.0

Is that it?

I believe so, but I am admittedly really noobish with Go. But yeah, I think that’s it.

1 Like

It looks like you found a solution. But I didn’t see a go get in your commands there. To be able to go get stuff, you only need to initialize a module and then you can run go get to your hearts’ content:

# Initialize a module
$ go mod init test.com/repo
go: creating new go.mod: module test.com/repo

# Show contents of module
$ cat go.mod
module test.com/repo

go 1.18

# Now that we have a module, we can run go get like in the book you're reading
$ go get github.com/mattn/go-sqlite3
go: downloading github.com/mattn/go-sqlite3 v1.14.14
go: added github.com/mattn/go-sqlite3 v1.14.14

# Module now requires github.com/mattn/go-sqlite3
$ cat go.mod
module test.com/repo

go 1.18

require github.com/mattn/go-sqlite3 v1.14.14 // indirect

Could ‘go mod tidy’ have done the ‘go get’ for me?
Or just found that it was already done (3 days earlier).
I think I witnessed such an implicit ‘go get’ for glot:

ch08> go run CSVplot.go /tmp/doesnotexist
CSVplot.go:6:2: no required module provides package github.com/Arafatk/glot: go.mod file not found in current directory or any parent directory; see 'go help modules'
ch08> go mod init CSVplot
ch08> go mod tidy
...
go: finding module for package github.com/Arafatk/glot
...
go: downloading github.com/Arafatk/glot v0.0.0-20180312013246-79d5219000f0
...
go: found github.com/Arafatk/glot in github.com/Arafatk/glot v0.0.0-20180312013246-79d5219000f0
...

Yeah - if you have referenced a module in your project, go mod tidy will automatically add it and go get it for you. From the docs:

go mod tidy ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s packages and dependencies, and it removes requirements on modules that don’t provide any relevant packages. It also adds any missing entries to go.sum and removes unnecessary entries.

As you can see, it will also remove modules that are no longer referenced. I use VSCode and often I’ll just add an import (like import "github.com/gorilla/mux") to the .go file I’m working on, then VSCode will know if I haven’t referenced it in go.mod and I can have it automatically go get the missing module.

Anyway, my comment was just in case you want to be able to use go get exactly how the book is telling you and follow along with the book exactly.

1 Like

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