如何使用 go.mod 和 go.sum 文件导入包含 Golang 代码的 Git 子模块?

huangapple go评论70阅读模式
英文:

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

问题

我在导入一个包含 go.mod 和 go.sum 文件的 golang 包的 gitsubmodule 到主项目目录时遇到了问题。但是在导入该包时遇到了问题。

gitsubmodule 包中的 go.mod 文件:

module abc

go 1.18

主项目目录中的 go.mod 文件:

module def

go 1.18

gitsubmodule go 包中的文件有导入语句:

package abc

import "abc/sample"

主项目包的文件:

package main

import "def/abc/sample"

我的项目代码结构如下:

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

但是在运行 main.go 文件时,import "abc/constant" 对于 gitsubmodule 报错导入错误。

英文:

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

答案1

得分: 1

解决这个问题的方法是在主要的 go.mod 文件中添加以下行:

replace abc => ./abc

然后执行 go mod tidy。

英文:

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

replace abc => ./abc

and do go mod tidy

huangapple
  • 本文由 发表于 2022年5月17日 21:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/72275167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定