dragoninasia2014 2018-11-15 23:47
浏览 97
已采纳

使用golang生成标头/声明文件-全部集中在一个文件中吗?

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:

entities/
    foo/
      get/
      put/
      post/
      delete/

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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?