多模块依赖管理

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

Go Multi Module Dependency Management

问题

我是新手,正在尝试开发一个多模块项目,我的工作空间文件夹结构如下:

root
1-Authz
1.1-Main.go
1.2-go.mod(包含:module com.mbt.authz)
1.3-go.sum
2-Product
2.1-Main.go
2.2-go.mod(包含:module com.mbt.product)
2.3-go.sum
3-go.work
4-GoMultiModule.code-workspace

go.work文件夹的结构如下:
go 1.18

use(
./Authz
./Product
)

这两个模块都可以独立运行。但是我想在Authz中定义一个方法,并从Product中调用该方法。我应该怎么做?如何在我的本地工作空间中为Product模块添加依赖?

英文:

I am new to goLang. I am tring to develop a multiModule project. my workspace folder is
like

root
   1-Authz
   1.1-Main.go
   1.2-go.mod (contains:module com.mbt.authz)
   1.3-go.sum
   2-Product
   2.1-Main.go
   2.2-go.mod  (contains:module com.mbt.product)
   2.3-go.sum
   3-go.work
   4-GoMultiModule.code-workspace

   go.work folder is like 
   go 1.18

   use(
    ./Authz
    ./Product
   )

Both modules can run by itself. But I want to define a method in Authz and call that
function from Product. What should I do, how can I add dependency to Product module from my local workspace?

答案1

得分: 2

为了进行测试,只需在Product/main.go中添加一个引用Authz的导入语句:

import com/mbt/authz/aPackage

这假设你在Authz项目中定义了一个与main不同的包中的方法。

英文:

For testing, simply add to Product/main.go an import referring to Authz:

import com/mbt/authz/aPackage

This assumes you define a method in a package different from main, in Authz project.

答案2

得分: 2

这是一个针对许多新的golang开发者的常见问题解答。

在golang中,模块(Module)和包(Package)不是同一回事。 一个模块可以包含一个或多个包。使用go mod init [模块名]命令初始化一个模块。该命令将创建一个go.mod文件。包可以在代码中简单地用package [包名]来定义。在同一个模块中的包(我们称之为本地包)可以使用import "[包名] [模块名]/[包路径]"来导入。

在你的情况下,如果你不想创建不同的模块,你可以在Authz和Products文件夹中删除go.mod和go.sum文件。然后在Root文件夹中运行go mod init root。然后在产品代码中使用类似import (authz "root/authz")的方式导入auth包(authz是在Authz代码中声明的包名)。

如果authz必须是一个不同的模块,它将被视为一个不同的模块,不能像本地包一样直接导入。

要在Authz文件夹中导入一个本地模块,你需要编辑products文件夹中的go.mod文件,像这样:

module 某个模块名

go 1.16

require (
	authz模块名 v0.0.0
)

replace authz模块名 v0.0.0 => ../Authz/
英文:

This is a FAQ for many new golang developer.

In golang, Module and Package are not the same thing. A module may contains one or more packages. Module is initialized with go mod init [modulename] command. This command will create a go.mod file. Package can be simply defined in code with package [packagename]. Package in the same module (let's call it local package) can be import with import "[packagename] [modulename]/[pathofpackage]"

In your case, if you don't want to create different module, you can delete the go.mod and go.sum file in both Authz and Products folder. Then in Root folder, run go mod init root. Then import auth package in product code with something like import (authz "root/authz") (authz is the package name delcared in Authz's code)

If authz has to be a different module, it will be treated as a different module which cannot be import diretly like a local package.

To import a local module in Authz folder, you need to edit the go.mod file in products folder like this:

module somemodulename

go 1.16

require (
	authzmodulename v0.0.0
)

replace authzmodulename v0.0.0 => ../Authz/

huangapple
  • 本文由 发表于 2022年4月9日 06:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71803835.html
匿名

发表评论

匿名网友

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

确定