Home > Back-end >  Add module as git submodule inside main package
Add module as git submodule inside main package

Time:10-25

I'm fairly new to golang, and have some issues creating a new module

I want to add a git submodule inside my main package so I can work and make commits to both packages at the same time

The module http_fs is added as a git submodule like this

git submodule add [email protected]:xxx/http_fs.git repo/http_fs

The main package

package main

import "repo/http_fs"

go.mod for http_fs module looks like this

module github.com/xxx/http_fs

go 1.19

When I try to run the main package with go run main.go I get this error

package repo/http_fs is not in GOROOT (/usr/local/go/src/repo/http_fs)

File structure

./main.go // main package
./repo/http_fs/http_fs.go

update

go.mod in the main package

module main

go 1.19

replace github.com/xxx/http_fs v1 => ./repo/http_fs

CodePudding user response:

The reason for the error

package repo/http_fs is not in GOROOT (/usr/local/go/src/repo/http_fs)

is that go.mod in /usr/local/go/src/repo/http_fs declares the modules github.com/xxx/http_fs, not repo/http_fs.

You need to import exactly the same module as specified in the go.mod, i.e. github.com/xxx/http_fs

In go.mod of your main module use replace directive:

replace github.com/xxx/http_fs v1.2.3 => ./repo/http_fs

Replace directive tells compiler where to find the sources of the module.

  •  Tags:  
  • go
  • Related