2018-11-15 23:47
浏览 30


I am looking to auto-generate some Golang declaration files based on some .json data. Ideally I could put all the declaration/header data into one file. But the way that Golang packages/namespacing works, I doubt I can do that.

Instead of Golang, using TypeScript, I can put a lot of TS types/declarations in one file, using namespaces like so:

export namespace Entities {
  export namespace Foo {

    export namespace GET {
      export namespace Basic {

        export interface Req { }
        export interface Res { }



    export namespace PUT {
      export namespace Basic {

        export interface Req { }
        export interface Res { }





my question is - is there a way to do something like this with golang? Or will I have to use separate files/folders to achieve separate namespaces?

The only thing I know how to do is put them in separate files:


and then in each of those folders, something like:

package get
type Basic struct {
     Req struct {}
     Res struct {}

but that doesn't achieve what I want unfortunately, because I cannot reference the Basic.Req or Basic.Res types directly.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangan6797
    duangan6797 2018-11-16 19:43

    Golang logically organizes code by packages. Each directory is a different package. If you want to generate structs in distinct packages, they must reside in different directories.

    You may want to organize your generated libraries by their language styles. The http package has good suggestions on how to organize a library with network calls.

    点赞 评论