How to import gitsubmodule containing golang code with go.mod and go.sum file

Issue

I am facing issue in importing a gitsubmodule containing golang package with go.mod and go.sum file inside the package inside main project directory. But facing issue in importing the package.

go.mod inside gitsubmodule package

module abc

go 1.18

go.mod inside main project directory

module def

go 1.18

files inside the gitsubmodule go package has imports

package abc

import "abc/sample"

file for main project package

package main

import "def/abc/sample"


the structure of my project code is like this:-

|── go.sum
|── go.mod
|── main.go import "def/abc"
abc
    ├── constant
    |    ├── constant.go
    ├── abc.go  ----> import "abc/constant"
    |── go.mod
    |── go.sum

and but import "abc/constant" giving problem for gitsubmodule saying import error when I try to run main.go file

Solution

the solution to this problem is inside the main go.mod file add the following
line

replace abc => ./abc

and do go mod tidy

Answered By – Mesc

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.