简体   繁体   中英

What is a naming standard for multi-word struct type in Go?

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?

edit:

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM