0

I have 2 possibly related issues.

I have a test monorepo setup, with 2 subdirectories (mod1 and mod2).
Each one of them has a go.mod file in them, and each of the modules has a single .go file with basic printing code. in mod2 there is a subdirectory mod2_lib (that holds one of those simple .go files with basic printing code), since I read that Go modules are basically their own little GOPATH's.

enter image description here

Id like to call function Run() thats in a package mod2/mod2_lib from mod1, but all Im getting is build github.com/account_name/test/mod1: cannot find module for path github.com/account_name/test/mod2/mod2_lib.

here are the files Im using to figure this out:

mod1/t.go

package main

import (
    "fmt"
    "github.com/account_name/test/mod2/mod2_lib"
)

func main() {
    fmt.Println("mod1")
    mod2_lib.Run()
}

mod2/mod2_lib/t_lib.go

package mod2_lib

import "fmt"

func Run() {
    fmt.Println("RUNS")
}

the second issue is that in this monorepo I'd like to have related Python and Rust code in top-level dirs py and rust. So I'd like to place all my Go packages in the go/src dir. How would other people import this go/src path into their project (possibly still having the "github.com/account_name/test/mod2/mod2_lib" as the import path, and not "github.com/account_name/test/go/src/mod2/mod2_lib")?

can anyone give me some pointers on these issues? I want to move to using Golang modules, and abandon the GOPATH.

deepblue
  • 8,426
  • 13
  • 48
  • 60
  • 1
    If the code is at `github.com/account_name/test/go/src/mod2/mod2_lib`, then that is the import path. That's how imports work. I'm not sure why the `src` part is in there, and it may cause you some issues (Go will detect it as a GOPATH). As to the compile error, have you done a `go get` or otherwise added `mod2` to the go.mod for `mod1`? – Adrian Jan 08 '19 at 16:40
  • ok I'll definitely remove the `src` part. but Im dealing with that after I get the above described simple case (all modules/packages are in the root dir) working – deepblue Jan 08 '19 at 16:50
  • @Adrian when I do a `go get` Im still getting `build github.com/account_name/test/mod1: cannot find module for path github.com/account_name/test/mod2/mod2_lib` – deepblue Jan 08 '19 at 16:54
  • @Adrian question, with this setup if I make a change in `mod2` locally, without pushing it to github.com, `mod1` is not detecting that change locally and its still using the remote version. Any idea on how to handle this with this module structure (outside the `GOPATH`). – deepblue Jan 08 '19 at 17:50
  • 1
    You can use `replace` to explicitly point at your local copy of the dependency: https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem – Adrian Jan 08 '19 at 17:55
  • if the local copy is not present (for people that are `get`/`build`-ing for the first time) will it still initially pull from github.com? – deepblue Jan 08 '19 at 17:58
  • Not if the `replace` is in the `go.mod` committed to the repo, no. So you'll want to change it only locally, only for testing changes in the dependency. – Adrian Jan 08 '19 at 18:03
  • thats what I thought. ok, I'll figure out how to best structure it. thanks @Adrian! much appreciated :) – deepblue Jan 08 '19 at 18:05

1 Answers1

-1

the issue was that it was a private github.com repo. making it public fixed it! :) have to figure out authentication now for the module system.

deepblue
  • 8,426
  • 13
  • 48
  • 60