gpt4 book ai didi

go - 如何通过 'go mod' 分隔不同目录中的代码?

转载 作者:行者123 更新时间:2023-12-01 22:40:38 24 4
gpt4 key购买 nike

我的源目录布局像

mywork/libA
mywork/libA/liba.go
mywork/libA/go.mod
mywork/progB
mywork/progB/go.mod
mywork/progB/progb.go
  • 在 mywork/libA/目录中,我输入 go mod init example.com/mywork/liba .
  • 在 mywork/progB/目录中,我输入 go mod init example.com/mywork/progb .

  • libA/liba.go
    package liba
    func Hi() string { return "hi" }

    libA/go.mod
    module example.com/mywork/liba

    go 1.13

    progB/progb.go
    package main
    import "example.com/mywork/liba"
    func main() { println("progb:", liba.Hi()) }

    progB/go.mod
    module example.com/mywork/progb

    go 1.13
    go buildlibA目录是可行的。但是 go build失败 progB并显示

    build example.com/mywork/progb: cannot load example.com/mywork/liba: cannot find module providing package example.com/mywork/liba



    如何纠正它?

    最佳答案

    progB/go.mod 应该在 https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem 中添加 require 和 replace 语句

    module example.com/mywork/progb

    require example.com/mywork/liba v0.0.0

    replace example.com/mywork/liba => ../libA

    go 1.13

    关于go - 如何通过 'go mod' 分隔不同目录中的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60348123/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com