douxishai8552 2019-07-05 11:39
浏览 43
已采纳

在具有多个模块的mono-repos中是否存在Go模块名称的约定?

In a multi-module repository, should a module name (set via the go.mod module directive) follow the conventions of package naming?

E.g. module github.com/org-name/repo-name/path/to/module-dir

I understand that, whatever the module is named, the packages within the module refer to each other using the module name as prefix. But, from outside the module, there seems to problems if the module name is set to something other than the <host><path-within-repo> pattern. get-ing a package included in the module then gives messages about unrecognized import path.

Is there any cause to name a module differently than <host><path-within-repo> ?

  • 写回答

2条回答 默认 最新

  • doujun7161 2019-07-05 12:57
    关注

    There isn't any hard requirement for referencing modules, although it always good practice to use the domain/repo pattern. So, if you want to reference other modules locally that are not in the GOPATH, you can use the replace directive.

    https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive

    replace also can be used to inform the go tooling of the relative or absolute on->disk location of modules in a multi-module project, such as:

    replace example.com/project/foo => ../foo

    Let's say we have the following structure:

    ├── .gitingore
    ├── pkg1
    │   ├── go.mod
    │   └── main.go
    └── pkg2
        ├── go.mod
        └── utils.go
    

    pkg1/main.go

    package main
    
    import (
        "fmt"
        "local/pkg2"
    )
    
    func main() {
        fmt.Println(pkg2.Add(1, 2))
    
    }
    

    pkg1/go.mod

    module local/pkg1
    
    go 1.12
    
    require local/pkg2 v0.0.0
    
    replace local/pkg2 => ../pkg2
    

    pkg2/utils.go

    package pkg2
    
    func Add(a, b int) int {
        return a + b
    }
    

    pkg2/go.mod

    module local/pkg2
    
    go 1.12
    

    Running:

    cd pkg1
    go run main.go
    

    You get:

    3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教