problems using external modules with go 1.16.4

127 views
Skip to first unread message

natxo....@gmail.com

unread,
May 7, 2021, 9:21:29 AM5/7/21
to golang-nuts
hi,

I am struggling with the new go version.

I have a simple script that imports a github module, and before (don't know which go version) I could just run:

go get github/user/module

and then in my crappy script I could

package main

func main() {
    p := module.New()
    p.run(whatever)
}

but this now exits with the error:
no required module provides package github.com/user/module: go.mod file not found in current directory or any parent directory; see 'go help modules'

I do not want to write a module, I just want to use a module. So how do I do this? I am reading all the tutorials  listed on the go help modules output, but at this point I do not need to write a module.

A bit confused. I am obviously doing it wrong, but I cannot understand why.

Any help greatly appreciated.

Regards,
Natxo





Jan Mercl

unread,
May 7, 2021, 9:34:14 AM5/7/21
to natxo....@gmail.com, golang-nuts
On Fri, May 7, 2021 at 3:21 PM natxo....@gmail.com
<natxo....@gmail.com> wrote:

> I do not want to write a module, I just want to use a module. So how do I do this? I am reading all the tutorials listed on the go help modules output, but at this point I do not need to write a module.

I don't know what "write a module" means, but to use a module (import
a package from within a module) your program needs the go.mod file.
Without it the build system does not and can not know where to get the
required module(s) from.

In the directory with your program do `$ go mod init example.com/foo`
(or whatever else) and `$ go mod tidy`. The last command figures out
the imported modules and writes them into the go.mod file with some
more info in the go.sum file. Then just `$ go build` should be able to
produce the executable.

Not tested, but that's what I recall.

Alex

unread,
May 7, 2021, 9:35:49 AM5/7/21
to golang-nuts
Was writing my reply then Jan beat me lol, anyway he didn't linked the tutorial soo...

natxo....@gmail.com

unread,
May 7, 2021, 9:51:41 AM5/7/21
to golang-nuts
aha, so now we always need to do this to track the dependencies. Yes, it works, it will take a few tries to get used to ;-), muscle memory.

Thanks to you and to Alex for a prompt, accurate response.

Regards,
Natxo
Reply all
Reply to author
Forward
0 new messages