-1

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.

Apepenkov
  • 415
  • 1
  • 3
  • 10
  • It looks like every package is its own module which is a definite code smell. Do you really need all these? In most cases these would all be one module. – Adrian Dec 15 '22 at 15:19
  • 1
    Note that I'm not suggesting changing project structure - keep your packages however you want, it just seems unnecessary to have every package be a module. – Adrian Dec 15 '22 at 15:20
  • 1
    "I want a project structure, looking like this" No, probably you don't. – Volker Dec 15 '22 at 15:23
  • There is circa 1900 types+functions currently, each with its own arguments and etc. I need to be able to split them into 'namespaces', e.g. `functions.messages` and so on. Is there a way to do it without modules? I need it from inside main file(s) of module, and from outside when importing. – Apepenkov Dec 15 '22 at 15:26
  • 2
    The `replace` directive only affects the current top-level module you are building from. Modules that are intended to be consumed as libraries should not require any `replace` directives. If you need to work with multiple modules locally, then look into [go workspaces](https://go.dev/doc/tutorial/workspaces). – JimB Dec 15 '22 at 15:45
  • 2
    The *namespace" in Go is the package. A module in Go is a set of packages with the same life cycle and doesn't provide any "namespace" at all. – Volker Dec 15 '22 at 15:45
  • 1
    Check [this answer out](https://stackoverflow.com/questions/60320844/can-i-have-multiple-packages-inside-a-single-go-module-how). I think you might want multiple packages in a single module. – Dean Dec 15 '22 at 16:35

1 Answers1

0

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.

Apepenkov
  • 415
  • 1
  • 3
  • 10