go module & local package

143 views
Skip to first unread message

Henry

unread,
Oct 9, 2019, 2:02:52 AM10/9/19
to golang-nuts
Hi,

I am having trouble trying to migrate from gopath to go module. Some of my packages reside in my local machine and are not published. So how do you get go module to import local-machine packages?

The second question is that do the dependencies need to use go module as well?

Do you need to create your go mod project outside of existing gopath?

Thanks.

Henry

Dimas Prawira

unread,
Oct 9, 2019, 2:07:06 AM10/9/19
to Henry, golang-nuts

Let me define this first modules are collections of packages. In Go 11, I use go modules like the following:

If both packages are in the same project, you could just do the following: In go.mod:

module github.com/userName/moduleName

and inside your main.go

import "github.com/userName/moduleName/platform"

However, if they are separate modules, i.e different physical paths and you still want to import local packages without publishing this remotely to github for example, you could achieve this by using replace directive.

Given the module name github.com/otherModule and platform, as you've called it, is the only package inside there. In your main module's go.mod add the following lines:

module github.com/userName/mainModule

require "github.com/userName/otherModule" v0.0.0
replace "github.com/userName/otherModule" v0.0.0 => "local physical path to the otherModule"

Note: The path should point to the root directory of the module, and can be absolute or relative.

Inside main.go, to import a specific package like platform from otherModule:

import "github.com/userName/otherModule/platform"

Here's a gentle introduction to Golang Modules


--
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/45e88d7b-9d10-4af5-a153-c27d4edb8956%40googlegroups.com.

Henry

unread,
Oct 10, 2019, 3:47:46 AM10/10/19
to golang-nuts
Thanks for the reply. It's been helpful.

It appears that godoc does not work with go module? Somehow my godoc only documents those packages in my gopath.

On Wednesday, October 9, 2019 at 1:07:06 PM UTC+7, Dimas Prawira wrote:

Let me define this first modules are collections of packages. In Go 11, I use go modules like the following:

If both packages are in the same project, you could just do the following: In go.mod:

module github.com/userName/moduleName

and inside your main.go

import "github.com/userName/moduleName/platform"

However, if they are separate modules, i.e different physical paths and you still want to import local packages without publishing this remotely to github for example, you could achieve this by using replace directive.

Given the module name github.com/otherModule and platform, as you've called it, is the only package inside there. In your main module's go.mod add the following lines:

module github.com/userName/mainModule

require "github.com/userName/otherModule" v0.0.0
replace "github.com/userName/otherModule" v0.0.0 => "local physical path to the otherModule"

Note: The path should point to the root directory of the module, and can be absolute or relative.

Inside main.go, to import a specific package like platform from otherModule:

import "github.com/userName/otherModule/platform"

Here's a gentle introduction to Golang Modules


On Wed, Oct 9, 2019, 1:03 PM Henry <henry.a...@gmail.com> wrote:
Hi,

I am having trouble trying to migrate from gopath to go module. Some of my packages reside in my local machine and are not published. So how do you get go module to import local-machine packages?

The second question is that do the dependencies need to use go module as well?

Do you need to create your go mod project outside of existing gopath?

Thanks.

Henry

--
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 golan...@googlegroups.com.

Igor Maznitsa

unread,
Oct 15, 2019, 3:30:27 PM10/15/19
to golang-nuts
mvn-golang also allows make multi-module go projects with locally situated modules, but it works through maven
Reply all
Reply to author
Forward
0 new messages