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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器