Module management drives me crazy

Hi, I never used GO before and I want to run a script I found on github.

the imports are

“github[DOT]com/golang/leveldb/db”
“github[DOT]com/golang/leveldb/table”

so I

go get github[DOT]com/golang/leveldb/db

which looks fine, but

go run script.go
script.go:11:2: no required module provides package github.com/golang/leveldb/db: go.mod file not found in current directory or any parent directory; see ‘go help modules’
script.go:12:2: no required module provides package github.com/golang/leveldb/table: go.mod file not found in current directory or any parent directory; see ‘go help modules’

and

go list github[DOT]com/golang/leveldb
no required module provides package github[DOT]com/golang/leveldb: go.mod file not found in current directory or any parent directory; see ‘go help modules’

There must be some basic missunderstanding on my side, having wasted about two hours already trying to search this on the web without being able to fix it or understand what I do wrong. So help me please.

PS: I needed to write the [DOT] substitution, because this board cleverly recognizes the imports as being URLs which I as a new user am only allowed to post two of.

Hey there,

Do you have a go.mod file in your directory? In case you don’t you can generate it with go mod init [your-project-name]. go.mod is the file that keeps track of the dependencies the project has, and then you’ll be able to go get ... packages, or, if they are already defined as imports in the code, you can run go mod tidy and they’ll be downloaded automatically for you.

2 Likes

Can I apply go.mod for any library?

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