Problem importing golang module which imports subpackage

Viewed 23

I have the following problem: I have a Go package in github with with following structure:

go.mod
go.sum
*.go
subpackage/
subpackage/*.go

The go.mod file has the module definition like:

module github.com/user/mypackage

and all the go files in the subpackage folder have the line

package subpackage

In one of the files in the root of the package mypackage I import the subpackage with

import "github.com/user/mypackage/subpackage"

If I run go mod tidy, go build ./..., go test ./... in the root of mypackage everything seems to work fine, however if import the mypackage in another module (different github repo) with:

import "github.com/user/mypackage"

I get the following error:

go: finding module for package github.com/user/mypackage
go: downloading github.com/user/mypackage v0.0.0-20220101000000-eaeaeaeaeaea
go: found github.com/user/mypackage in github.com/user/mypackage v0.0.0-20220101000000-eaeaeaeaeaea
go: finding module for package github.com/user/mypackage/subpackage
github.com/some-other-module imports
        github.com/user/mypackage imports
        github.com/user/mypackage/subpackage: module github.com/user/mypackage@latest found (v0.0.0-20220101000000-eaeaeaeaeaea), but does not contain package github.com/user/mypackage/subpackage

Why is this happening? Since there is not circular dependency and the github.com/user/mypackage module definitely containts package github.com/user/mypackage/subpackage. I don't want to split it in two separate modules.

go version go1.18.5 linux/amd64

0 Answers
Related