duanbairan4235 2019-03-29 12:41
浏览 78


I am reading mixed opinions on how to name structs and the files that contain them. I'm also having a hard time finding detail on multi-word structs.

What is the most standard way to name my structs and the files that contain them in the following project?

I have an executable project with 2 structs:

  • Foo
  • FooBar

I want to declare the structs in their own file so I can create test files. Each will get imported into main.go.

  • Do I name the structs PascalCase? if not, how?
  • How should the go files be named?


This doc is why I went with Pascal Case first, but it is a one word struct and doesn't show it being used in a separate file. https://tour.golang.org/moretypes/2

  • 写回答

1条回答 默认 最新

  • dongquan8753 2019-03-29 18:07

    The effective go blog post in the official Golang blog helps a lot with this. The Package Names section and the Mixed Caps as suggested by icza are specially helpful.

    I also had this discussion with the community in the official slack channel a couple months ago, and most agreed that files should be always lowercase. If the names are too big, you can use underscores , be be careful: the go build will ignore files starting with _, and don't use specific suffixes, like _test.go and OS/arch names, like _amd64.go or _linux.go. The build tool will only use those last ones in the specific OSs/architectures. This is specified the the build docs.

    So if you are creating a struct called FooBar, your file can either be foobar.go or foo_bar.go.

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



    • ¥15 I350 Gigabit Network
    • ¥15 关于#abap#的问题,请各位专家解答!
    • ¥20 内网通过公网访问外网问题
    • ¥20 谁有这个东西 继续教育的
    • ¥15 怎么使请求通过cors
    • ¥15 WDM 驱动ACPI 相关疑问
    • ¥15 prism 跨窗体共享数据绑定 wpf
    • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
    • ¥15 0基础计算机毕设,应该从哪开始?
    • ¥60 使用DKT40脑图划分ROI区域