developing local packages with modules

71 views
Skip to first unread message

Erwin Driessens

unread,
Jun 7, 2020, 9:20:40 AM6/7/20
to golang-nuts
Hello people
i have always found modules very scary and complicated but now there seems to be no way round any longer.
I have a lot of packages that i do now want to put in repositories. I want them to be locally accessible, without internet access. 
Everything always worked great for me with the old GOPATH setup. 
Yesterday i installed go 1.14 on a new machine, and thought, ok lets try the modules. I followed "How to write Go code" (https://golang.org/doc/code.html) and was happy to read the following:
"
Note that you don't need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it's a good habit to organize your code as if you will publish it someday.
"
I did indeed get the hello module to work well.

However, my next quest was to import the hello/morestrings package in another module and use it there. I can get it to work :(
Does anyone know of a good document/wiki/tutorial about developing go code that is not on remote repositories?  Go was great but now i feel totally handicapped...

 



Erwin Driessens

unread,
Jun 7, 2020, 9:31:49 AM6/7/20
to golang-nuts
should be: "i can't get it to work", instead of "i can get it to work", sadly enough

Michael Stiller

unread,
Jun 7, 2020, 9:32:16 AM6/7/20
to Erwin Driessens, golang-nuts
Hi,

if the you want to use is on the local system you can add something like this to the go.mod file:

replace github.com/yourrepo/module => ../pkg/module

See also here:

https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

and here:

https://starkandwayne.com/blog/switching-to-go-modules/

Best regards,

Michael
> --
> 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/186c785e-f328-439b-a8e5-87f6f0faba90o%40googlegroups.com.

--
2scale GmbH, Schanzenstr. 20, 40549 Düsseldorf
Amtsgericht: Düsseldorf HRB 50718
Geschäftsführer: Georg von Zezschwitz, Dirk Vleugels
USt-IdNr.: DE 210936505





Erwin

unread,
Jun 7, 2020, 9:36:40 AM6/7/20
to Michael Stiller, golang-nuts
Thanks Micheal, i will read those articles.
But won't it be very cumbersome to have to edit the go.mod file all the time? It seems like it would break the flow.


Brian Candler

unread,
Jun 7, 2020, 10:06:48 AM6/7/20
to golang-nuts
On Sunday, 7 June 2020 14:20:40 UTC+1, Erwin Driessens wrote:

However, my next quest was to import the hello/morestrings package in another module and use it there. I can['t] get it to work :(
Does anyone know of a good document/wiki/tutorial about developing go code that is not on remote repositories?  Go was great but now i feel totally handicapped...


If you used "github.com/me/hello" as the base project, then use "github.com/me/hello/morestrings" for the sub-package in the "morestrings" subdirectory.

==> go.mod <==

go 1.14

==> main.go <==
package main

import (
    "fmt"
)

func main() {
    fmt.Println(morestrings.Greeting)
}

==> morestrings/strings.go <==
package morestrings

const Greeting = "Hello, world!"

Result:

$ go build
$ ./hello
Hello, world!

Note: you don't need to use "package morestrings" inside the "morestrings" directory - this is just a convention. The "import" statement points to the directory, but the package defined in that directory can have any name.  The following also works:

==> go.mod <==

go 1.14

==> main.go <==
package main

import (
    "fmt"
)

func main() {
    fmt.Println(wibble.Greeting)
}

==> morestrings/strings.go <==
package wibble

const Greeting = "Hello, world!"

Erwin

unread,
Jun 7, 2020, 11:01:14 AM6/7/20
to Brian Candler, golang-nuts
Yes Brian, this the way it is done in the "How to write Go code" article, and it indeed works. At least, when package
morestrings is imported in the hello module where it is part of. I did get that to work, but... i wanted to take the next
step and import that package morestrings in a whole new module, let's call it hello2. I can't get that to work without
actually creating a real repository and getting it from there. Or, i am now finding out, after editing my .mod file to use
replace. I had hoped there would be a way to not have to manually edit the .mod files. Go development used to be 
so beautifully simple!
  


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/9-5aDopSGvo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7627c8fd-9b17-4863-88aa-d278c70d8106o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages