登录
首页 >  Golang >  Go问答

如何在 golang 的 go.mod 中正确使用递归替换?

来源:stackoverflow

时间:2024-02-12 13:39:23 349浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《如何在 golang 的 go.mod 中正确使用递归替换?》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我想要一个项目结构,如下所示:

src/github.com/myname/myproject/
- tl:
    - constructor
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/constructor

            require github.com/myname/myproject v0.0.0
            require github.com/myname/myproject/tl/types v0.0.0
            require github.com/myname/myproject/tl/types/account v0.0.0
            require github.com/myname/myproject/tl/types/auth v0.0.0
            require github.com/myname/myproject/tl/types/bots v0.0.0

            replace github.com/myname/myproject v0.0.0 => ./../..
            replace github.com/myname/myproject/tl/types v0.0.0 => ./../types
            replace github.com/myname/myproject/tl/types/account v0.0.0 => ./../types/account
            replace github.com/myname/myproject/tl/types/auth v0.0.0 => ./../types/auth
            replace github.com/myname/myproject/tl/types/channels v0.0.0 => ./../types/channels
    - crcs
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/crcs

    - functions
        - account
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/account

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - auth
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/auth

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - bots
            - someghing.go
            - go.mod:
                module github.com/myname/myproject/tl/functions/bots

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/functions

            require github.com/myname/myproject/tl/crcs v0.0.0
            require github.com/myname/myproject v0.0.0
            require github.com/myname/myproject/tl/types v0.0.0

            replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs
            replace github.com/myname/myproject v0.0.0 => ./../..
            replace github.com/myname/myproject/tl/types v0.0.0 => ./../types
    - types
        - account
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/account

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..

        - auth
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/auth

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..
        - channels
            - something.go
            - go.mod:
                module github.com/myname/myproject/tl/types/channels

                require github.com/myname/myproject/tl/crcs v0.0.0
                require github.com/myname/myproject v0.0.0
                require github.com/myname/myproject/tl/types v0.0.0

                replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs
                replace github.com/myname/myproject v0.0.0 => ./../../..
                replace github.com/myname/myproject/tl/types v0.0.0 => ./..
        - someghing.go
        - go.mod:
            module github.com/myname/myproject/tl/types

            require github.com/myname/myproject/tl/crcs v0.0.0
            require github.com/myname/myproject v0.0.0

            replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs
            replace github.com/myname/myproject v0.0.0 => ./../..
- something.go
- go.mod:
    module github.com/myname/myproject

    [some external dependencies]
    require github.com/myname/myproject/tl/types v0.0.0
    replace github.com/myname/myproject/l/types v0.0.0 => ./tl/types

这不起作用。我使用 jetbrains 的 ide,它在所有(子)模块上执行 go list,并告诉我以下错误:

go: github.com/myname/myproject/tl/[email protected] requires
    github.com/myname/myproject/tl/[email protected]: reading github.com/myname/myproject/tl/crcs/go.mod at revision tl/crcs/v0.0.0: unknown revision tl/crcs/v0.0.0

我不明白为什么会发生这种情况,看起来它尝试从存储库中获取模块,即使所有内容都在本地替换。 除了改变项目结构之外,我愿意接受建议。我将来需要能够使用 account.sometype 之类的东西,因此将所有内容都放在一个文件中会很痛苦。

上下文:我正在为电报构建 mtproto 库,它具有 tl 模式,其中每个“请求”/“类”都在自己的命名空间中(例如“帐户”、“通道”)(或在全局命名空间中,例如我是 functionstypes 相应)(因为这些函数/类有超过 1900 个,每个函数/类都有自己的构造函数和参数,因此将所有内容都放在一个模块中将很难使用)

tl文件夹中的文件是从tl模式自动生成的go代码。


正确答案


感谢Dean指出您可以在一个模块中包含多个包(如何?)。它解决了我的问题,同时仍然保持相同的结构。

今天关于《如何在 golang 的 go.mod 中正确使用递归替换?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>