Home > Net >  How do I import a project within a Go Workspace?
How do I import a project within a Go Workspace?

Time:06-29

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?

CodePudding user response:

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

CodePudding user response:

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

  • Related