still struggling to understand modules

377 views
Skip to first unread message

rob

unread,
Feb 23, 2021, 8:49:21 PM2/23/21
to golang-nuts
Hi.  I'm a hobby programmer.  I've been able to learn and use gopath
mode for a while.  I've accumulated 20+ programs that I use that were
build using gopath mode.  I'm not using a package manager, as all my
code is in GOPATH, ie, ~/go/src/

I don't understand how to convert this to modules.  I've read the blogs
on golang.org/ref/mod# ... , and still don't understand how to convert
to modules.

I tried a year or so ago, but I then could not compile anything, so I
nuked whatever I did and reverted to GOPATH.

I'm looking for a conversion guide for the complete idiot, or for
dummies if that's preferable.

I'd be satisfied if there was a step by step guide for converting
~/go/src/hello.go to modules.  I could probably adapt that.

Right now, all documentation I've seen starts by doing git clone
https://github.com/my-own-code.  And then I'm lost.

If this is not the correct forum to post this very basic request for
help, guidance as to where else I should GO would be helpful.

--rob solomon


Amit Saha

unread,
Feb 23, 2021, 9:33:45 PM2/23/21
to r...@drrob1.com, golang-nuts
What is your current Go version?

You mention:

~/go/src/hello.go

Is that the exact path or do you have ~/go/src/<some-dir>/hello.go?

When you say you have 20+ programs that I use that were build using
gopath mode, how is the source structure exactly on your system?




>
> --rob solomon
>
>
> --
> 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 on the web visit https://groups.google.com/d/msgid/golang-nuts/24b2ccb9-f4f5-83b0-6a1b-6774021ce029%40gmail.com.



--
Software Engineer, Author
https://echorand.me

Ian Lance Taylor

unread,
Feb 23, 2021, 10:18:05 PM2/23/21
to r...@drrob1.com, golang-nuts
On Tue, Feb 23, 2021 at 5:49 PM rob <drro...@gmail.com> wrote:
>
> I'm looking for a conversion guide for the complete idiot, or for
> dummies if that's preferable.

Does this article from the Go blog help?

https://blog.golang.org/migrating-to-go-modules

Ian

Nick Keets

unread,
Feb 24, 2021, 2:49:37 AM2/24/21
to r...@drrob1.com, golang-nuts
Hello rob, here's what I do.

For every dir in ~/go/src run go mod init with the dir name.

e.g.:
cd ~/go/src/foo
go mod init foo
cd ~/go/src/bar
go mod init bar
...

If package foo depends on libfoo (also in ~/go/src) you can add a replace line in foo's go.mod
e.g.
echo "replace libfoo => ../libfoo" >> ~/go/src/foo/go.mod

Then foo can import libfoo with:
    import "libfoo"

Before building for the first time run "go get" to download all dependencies and update the go.mod file.

After that, you won't need GOPATH or having everything in ~/go anymore.

Hope that helps.

rob

unread,
Feb 24, 2021, 8:24:33 PM2/24/21
to Amit Saha, r...@drrob1.com, golang-nuts
I was unclear.

My directories are structured so that each is its own program. For
example, I have programs w/ these names, and the code is in that
directory.  That's how I understood GOPATH was supposed to be organized

~/go/src/dsrt

~/go/src/regex

~/go/src/comparehashes

There are at least 20 of these.  I have compiled these from within
~/go/src using

  go install dsrt

  go install regex

and so on


Additionally, I would like more explanation and/or documentation as to
the replace command in a go.mod file.


I'm also curious as to WHY GOPATH is being removed.  That would
immediately make obsolete books like "The Go Programming Language" by
Alan Donovan and Brian Kerninghan, because none of their code would
compile as is.

However, it does provide opportunities for people to write more books

--rob solomon

rob

unread,
Feb 24, 2021, 8:46:36 PM2/24/21
to Miguel Angel Rivera Notararigo, r...@drrob1.com, golang-nuts
And another question.  Some of my code is defined as

package main

and other code directories are

package calc as it is intended to be imported by other of my code packages.

Am I expected to run

go mod init <module name>

on all of my code, or only those that are imported by package main code?

Please forgive my denseness

--rob solomon

Ralph Seichter

unread,
Feb 24, 2021, 9:44:41 PM2/24/21
to golan...@googlegroups.com
* rob:

> I'm also curious as to WHY GOPATH is being removed.

Because GOPATH forced people to store all their Go code under a specific
directory, which is a hindrance. go.mod allows users to choose different
paths for different projects. It also enables programmers to use Git or
other VCS to store their source code and dependency definitions in
separate directories.

-Ralph

Ian Lance Taylor

unread,
Feb 24, 2021, 10:24:16 PM2/24/21
to r...@drrob1.com, Amit Saha, golang-nuts
On Wed, Feb 24, 2021 at 5:24 PM rob <drro...@fastmail.com> wrote:
>
> I was unclear.
>
> My directories are structured so that each is its own program. For
> example, I have programs w/ these names, and the code is in that
> directory. That's how I understood GOPATH was supposed to be organized
>
> ~/go/src/dsrt
>
> ~/go/src/regex
>
> ~/go/src/comparehashes
>
> There are at least 20 of these. I have compiled these from within
> ~/go/src using
>
> go install dsrt
>
> go install regex
>
> and so on
>
>
> Additionally, I would like more explanation and/or documentation as to
> the replace command in a go.mod file.

Most likely you should run "go mod init" in each directory.

> Am I expected to run
>
> go mod init <module name>
>
> on all of my code, or only those that are imported by package main code?

In each directory.

Ian

rob

unread,
Feb 25, 2021, 7:40:30 AM2/25/21
to Ian Lance Taylor, r...@drrob1.com, golang-nuts
Thanks for answering me.

Now I have a curious problem.  Most of the code I've written is
compiling.  I'm using go 1.15.8 under ubuntu.  I did

    I'm logged into ~/go/src

    export GO111MODULE=on

    cd multack            This is my code

    go mod init multack

    cd ..

    Now I'm back in ~/go/src

    go install multack

I get an error saying that "package multack is not in GOROOT
(usr/local/go/src/multack)"

If I do

    go run multack/multack.go

That works.

What does this error message mean?

Thanks again.

--rob solomon

rob

unread,
Feb 25, 2021, 6:29:36 PM2/25/21
to r...@drrob1.com, golang-nuts
I solved my issue by restarting the terminal program.  The code now
compiles.  And I also tested on Win10 and go 1.16.

I'm glad I asked my question, because nothing I read said that all I had
to do to use modules is to go into each code directory I use and

   go mod init <module-name>


Perhaps that simple instruction could be added to the documentation?

It would help complete idiots (or dummies) like me.

--rob solomon


---------------------------------------------------

rob

unread,
Feb 26, 2021, 7:27:09 AM2/26/21
to r...@drrob1.com, golang-nuts
Nope.  I didn't solve it.  The code compiles because of GO111MODULE=auto
and I did not notice that when I posted yesterday.  The code does not
compile when GO111MODULE=on

The simple instructions that I suggest be added to the documentation
would benefit from having a section called

"Common error messages and how to fix them"

One of these errors that the documentation would be improved by
addressing would be

    package packagename is not in GOROOT (usr/local/go/src/packagename)


Another could be

     go: cannot find main module, but found .git/config in
/home/name/go/src

     to create a module there run

     go mod init


When I try "go mod init" in my /home/rob/go/src, I get an error

go: cannot determine module path for source directory /home/rob/go/src
(outside GOPATH, module path must be specified)


How do I specify my local code?


The documentation is weak in these areas.


-----------------------------------------------------

Brian Candler

unread,
Feb 26, 2021, 8:40:15 AM2/26/21
to golang-nuts
On Friday, 26 February 2021 at 12:27:09 UTC rob wrote:
When I try "go mod init" in my /home/rob/go/src, I get an error

go: cannot determine module path for source directory /home/rob/go/src
(outside GOPATH, module path must be specified)


What it means is, you must give a name (import path) for your package.

go mod init example

will do.  "go mod init github.com/myuser/myproject" is better, if you intend to publish your project on github for others to be able to import.

However in your case,

go mod init multack

would have been fine.  To install it, you should just do "go install" (or "go install .") in the same directory - not cd out to an outer directory.  Then you'll find it creates a "multack" binary in ~/go/bin

To avoid confusion, do all of this completely outside the ~/go tree.  Just forget that ~/go/src exists.

$ cd
$ mkdir zzz
$ cd zzz
$ go mod init multack
go: creating new go.mod: module multack
$ echo 'package main; func main() {}' >main.go
$ go install
$ ls -l ~/go/bin/multack
-rwxr-xr-x  1 brian  staff  1203088 26 Feb 13:37 /Users/brian/go/bin/multack
Reply all
Reply to author
Forward
0 new messages