doupingtang9627 2014-10-07 05:01
浏览 38
已采纳

Golang-具有结构,变量和接口的代码组织

I have a codebase where one file contains quite a lot of Structs, Interfaces and Variables in the same file as functions and I'm not sure if I need to seperate this into separate files with appending filename. So for example accounts.go will be accounts_struct.go and accounts_interface.go with struct and interface respectively.

What would be a good approach for the file organisation when you have growing codebase for Structs, Variables and Interfaces?

  • 写回答

2条回答 默认 最新

  • duangou2028 2014-10-07 06:34
    关注

    A good model to check out is the source code of Go itself: http://golang.org/src/pkg/

    You will see that this approach (separating based on language items like struct, interface, ...) is never used.

    All the files are based on features, and it is best to use a proximity principle approach, where you can find in the same file the definition of what you are using.
    Generally, those features are grouped in one file per package, except for large ones, where one package is composed of many files (net, net/http)

    If you want to separate anything, separate the source (xxx.go) from the tests/benchmarks (xxx_test.go)


    As Thomas Jay Rush adds in the comments

    Sometimes source code is automatically generated -- especially data structure definitions.
    If the data structures are in the same file as the hand-wrought code, one must build capacity to preserve the hand-wrought portion in the code-generation phase.
    If the data structures are separated in a different file, then inclusion allows one to simply write the data structure file without worry.


    Dave Cheney offers an interesting perspective in "Absolute Unit (Test) @ LondonGophers" (March 2019)

    You should take a broader view of the "unit" under test.
    The units are not each internal function you write, but a whole package. Specifically the public API of a package.

    Organizing your files to facilitate testing their Public API is a good idea.
    accounts_struct_test.go would not, in that regards, make much sense.


    See also "How I organize packages in Go" by Bartłomiej Klimczak

    Sometimes, a few handlers or repositories are needed.
    For example, some information can be stored in a database and then sent via an event to a different part of your platform. Keeping only one repository with a method like saveToDb() isn’t that handy at all.
    All of elements like that are split by the functionality: repository_order.go or service_user.go.
    If there are more than 3 types of the object, there are moved to a separate subfolder.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题