首页  >  文章  >  后端开发  >  如何在 golang 的 go.mod 中正确使用递归替换?

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

WBOY
WBOY转载
2024-02-08 23:50:36595浏览

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

在使用golang的go.mod时,正确使用递归替换是一个重要的问题。php小编鱼仔为您提供了一些解决方案。递归替换是指在go.mod文件中将某个模块替换为本地目录的方式,以便在开发过程中进行调试和修改。但是,如果替换的模块依赖其他模块,可能会导致一些问题。通过正确使用递归替换,您可以避免这些问题,保证项目的依赖关系正常运行。在本文中,我们将讨论如何在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 中正确使用递归替换?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除