2

I have a monorepo with multiple modules within it. Some of the modules are libraries, others are entry points that will produce binaries.

Here's my workspace project: https://github.com/alshdavid/go-workspace-example

I have my "bar" library here which I would like to make available for import by consumers outside of this mono repo.

package main

import (
    "fmt"

    "github.com/alshdavid/bar"
    // Or maybe: "github.com/alshdavid/gotest/modules/bar"
)

func main() {
    fmt.Println(bar.Bar)
}

Trying to install the package from the workspace git repo results in failure.

go get github.com/alshdavid/gotest
go get github.com/alshdavid/gotest/modules/bar
go get github.com/alshdavid/bar # worth a shot

Is there an argument I should pass to go get that instructs to to look for a module in a nested folder or should I instead set up a pipeline that pushes my nested module to its own repository?

David Alsh
  • 6,747
  • 6
  • 34
  • 60

2 Answers2

1

You must be working inside a module i.e. ran: go mod init in each of your modules in your mono repo. For example, in your case I am assuming you have module named bar and you want to import module bar inside another module e.g. foo.

A module has a go.mod file. Inside go.mod file of your foo module you can add the following:

replace github.com/alshdavid/bar => ../bar

You can read more about this here

  • Thanks for the response. Yes the `bar` module has a `go.mod` but the repository has a `go.work` denoting that it's a Go workspace. From a module that exists in an external repository (outside the go workspace), I cannot access the library that lives within the aforementioned Go workspace – David Alsh Jun 29 '22 at 10:20
-2

I just needed the complete path in the go.mod file

David Alsh
  • 6,747
  • 6
  • 34
  • 60