workspace question

168 views
Skip to first unread message

Steve Roth

unread,
Oct 12, 2022, 2:49:28 AM10/12/22
to golang-nuts
I'd appreciate help with setting up a workspace, involving two modules that exist only on my local disk and not in any SCM.  I understand how to create the workspace and use both modules in it.  What I can't figure out is how to add a dependency from mod1 to mod2 in mod1's go.mod file.

The supported means of adding dependencies in go.mod files is the go get command.  But if I go into mod1's directory and run "go get path/to/mod2", it tries to download it from github and fails.  I cannot figure out how to tell go get to use the version that's on my local disk, even though the workspace says it should.

Similarly, I can't figure out how to add the necessary go.mod and go.sum entries manually.  The documentation explicitly warns against trying to do so, anyway.

It seems like this is exactly the case workspaces were designed for, developing two modules at once, and yet I've had no success in getting it set up.  Any suggestions?

Regards,
Steve

Jan Mercl

unread,
Oct 12, 2022, 6:23:19 AM10/12/22
to Steve Roth, golang-nuts
For example:

jnml@3900x:~/tmp/modules/bar$ rm -rf *
jnml@3900x:~/tmp/modules/bar$ go mod init example.com/bar
go: creating new go.mod: module example.com/bar
jnml@3900x:~/tmp/modules/bar$ echo 'package bar; func Y() {}' > bar.go
jnml@3900x:~/tmp/modules/bar$

and

jnml@3900x:~/tmp/modules/foo$ rm -rf *
jnml@3900x:~/tmp/modules/foo$ go mod init example.com/foo
go: creating new go.mod: module example.com/foo
jnml@3900x:~/tmp/modules/foo$ echo 'package foo; import "example.com/bar"; func X() { bar.Y() }' > foo.go
jnml@3900x:~/tmp/modules/foo$ go build -v
foo.go:1:21: no required module provides package example.com/bar; to add it:
go get example.com/bar
jnml@3900x:~/tmp/modules/foo$ go work init
jnml@3900x:~/tmp/modules/foo$ go work use ../bar
jnml@3900x:~/tmp/modules/foo$ go build -v
directory . is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use .
jnml@3900x:~/tmp/modules/foo$ go work use .
jnml@3900x:~/tmp/modules/foo$ go build -v
jnml@3900x:~/tmp/modules/foo$


HTH

-j

Steve Roth

unread,
Oct 12, 2022, 2:01:16 PM10/12/22
to Jan Mercl, golang-nuts
Thank you, Jan.  Apparently where I went wrong was assuming that the dependency had to be listed in go.mod.  (i.e., using the names from your example, foo/go.mod needed to have a "require bar" in it).  That is what I was struggling to achieve; it never occurred to me that it could be omitted.  I'm still surprised it works without that; it seems weird to have a dependency on another module and not list it with all of the other dependencies.

At any rate, I have it working now based on your help — much appreciated.
Steve

Reply all
Reply to author
Forward
0 new messages