如何在Go语言的go.mod文件中正确使用递归替换?

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

How do I properly use recursive replace in golang's go.mod?

问题

我想要一个项目结构,看起来像这样:

src/github.com/myname/myproject/
- tl:
    - constructor
        - something.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
        - something.go
        - go.mod:
            module github.com/myname/myproject/tl/crcs

    - functions
        - account
            - something.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
            - something.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
            - something.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
        - something.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 => ./..
        - something.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

    [一些外部依赖]
    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/types@v0.0.0 requires
	github.com/myname/myproject/tl/crcs@v0.0.0: 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的东西,所以将所有内容放在一个文件中将很痛苦。

背景:我正在为Telegram构建MTPROTO库,其中包含TL模式,每个“请求”/“类”都在自己的命名空间中(例如“account”,“channels”)(或在全局命名空间中,对我来说是functionstypes)(因为有超过1900个这样的函数/类,每个都有自己的构造函数和参数,将所有内容放在一个模块中将很难使用)

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

英文:

I want a project structure, looking like this:

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

This does not work. I use JetBrains' IDE, which executes go list on all the (sub)modules, and tells me the following error:

go: github.com/myname/myproject/tl/types@v0.0.0 requires
	github.com/myname/myproject/tl/crcs@v0.0.0: reading github.com/myname/myproject/tl/crcs/go.mod at revision tl/crcs/v0.0.0: unknown revision tl/crcs/v0.0.0

I don't understand why is it happening, it looks like it tries fetching module from repo, even though everything is replaced locally.
I am open to suggestions, except changing project structure. I need to be able to use something like account.SomeType in future, so placing everything in one file would be painful.

Context: I'm building MTPROTO library for telegram, which has TL schema, where each "request"/"class" is in its own namespace (e.g. "account", "channels") (or in global namespace, for me it's functions and types accordingly) (Because there is over 1900 of those functions/classes, each having its own constructor and parametrs, putting everything in one module would be hard to use)

the files in TL folder are auto-generated GO code from TL schema.

答案1

得分: 0

感谢Dean指出了一个事实,即你可以在一个单一模块中拥有多个包(如何实现?)。这解决了我的问题,同时保持了相同的结构。

英文:

Thanks for Dean for pointing out the fact that you can have multiple packages in a single module (how?). It solved my problem while still maintaining same structure.

huangapple
  • 本文由 发表于 2022年12月15日 23:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/74813689.html
匿名

发表评论

匿名网友

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

确定