Go语言中的文件命名约定是什么?

huangapple go评论93阅读模式
英文:

What are conventions for filenames in Go?

问题

我可以找到关于Go语言包命名的约定:单词之间不使用下划线,全部小写。

这个约定是否也适用于文件名?

你是否也将一个结构体放在一个文件中,就像你为Java类所做的那样,然后以结构体的名称命名文件?

目前,如果我有一个名为WebServer的结构体,我会将它放在一个名为web_server.go的文件中。

英文:

I could find the conventions for naming packages in Go: no underscore between words, everything lowercase.

Does this convention apply to the filenames too?

Do you also put one struct in one file as if you did for a java class and then name the file after the struct?

Currently, if I have a struct WebServer, I put it in a file web_server.go.

答案1

得分: 199

有一些遵循的准则。

  1. 以“.”或“_”开头的文件名将被go工具忽略。
  2. 后缀为_test.go的文件只会被go test工具编译和运行。
  3. 具有特定操作系统和架构后缀的文件将自动遵循相同的约束条件,例如name_linux.go只会在Linux上构建,name_amd64.go只会在amd64上构建。这与在文件顶部有一行//+build amd64的效果相同。

有关更多详细信息,请参阅go文档:https://pkg.go.dev/cmd/go

英文:

There's a few guidelines to follow.

  1. File names that begin with "." or "_" are ignored by the go tool
  2. Files with the suffix _test.go are only compiled and run by the go test tool.
  3. Files with os and architecture specific suffixes automatically follow those same constraints, e.g. name_linux.go will only build on linux, name_amd64.go will only build on amd64. This is the same as having a //+build amd64 line at the top of the file

See the go docs for more details: https://pkg.go.dev/cmd/go

答案2

得分: 59

除了JimB提供的答案之外,常规文件名应该是小写的,简短的,没有任何下划线或空格。通常,文件名遵循与包名相同的约定。请参阅《Effective Go》中的《Package Names》部分。

请参阅《strconv package》以获取一个很好的示例。

英文:

In addition to the answer provided by JimB, regular file names are lower case, short, and without any sort of underscore or space. Generally, file names follow the same convention as package names. See the Package Names section of Effective Go.

See the strconv package for a good example.

答案3

得分: 27

Go在包内组织代码方面非常自由,通常是为了提高代码的可读性和理解性。学习如何做到这一点的最佳方法是研究专家们的代码,即浏览标准库:

http://golang.org/src/pkg/

然而,我可以想到两个规则。当指定要编译到不同平台的代码时,您可以使用平台名称作为后缀:

mypkg_linux.go         // 仅在Linux系统上构建
mypkg_windows_amd64.go // 仅在Windows 64位平台上构建

此外,如果您有一个名为server.go的文件,该文件的测试应该在server_test.go中。

英文:

Go is quite liberal in terms of how you organise your code within a package, usually it's whatever improves readability and understanding of your code. The best way to learn how this is done is to study the masters, i.e. have a browse though the standard library:

http://golang.org/src/pkg/

There are 2 rules I can think of however. When specifying code to be compiled for different platforms, you use the platform name as a suffix:

mypkg_linux.go         // only builds on linux systems
mypkg_windows_amd64.go // only builds on windows 64bit platforms

Also if you have a file called server.go, the tests for that file would be in server_test.go.

答案4

得分: 11

通常,在文件名中使用下划线来指定特定平台/架构的代码,例如:

➜ cd $GOROOT/src/pkg/math/
➜ ls sqrt*s
sqrt_386.s  sqrt_amd64p32.s  sqrt_amd64.s  sqrt_arm.s

sqrt_386.s 只会在32位处理器上被编译器读取,sqrt_amd64.s 在amd64上,依此类推。

它可以是 GOOS 和/或 GOARCH 的任何有效值(参考:http://golang.org/doc/install/source#environment)。

file_windows_amd64.go 只会在win64上编译。

英文:

Usually underscore in filenames are used to assign platform/arch-only code, for example:

➜ cd $GOROOT/src/pkg/math/
➜ ls sqrt*s
sqrt_386.s  sqrt_amd64p32.s  sqrt_amd64.s  sqrt_arm.s

sqrt_386.s will only be read by the compiler on 32bit processors, sqrt_amd64.s on amd64, etc..

It can be any of the valid values of GOOS and/or GOARCH (ref.

file_windows_amd64.go will be only compiled on win64.

答案5

得分: 4

文件名全小写是一个糟糕的主意!

文件名在Go中没有语义意义-它们不被导入或引用。Go中的代码文件被编译为它们所属的包作为一个单元。

由于effective go中的文章没有包括对多个单词文件名的限制,除了下划线(用于构建、忽略、测试和编译目的),mixedCaps是多个单词文件名约定的最佳选择。同样的文章"effective go"也推荐使用mixedCaps。

简而言之:

- smallallcupspackagename
|- veryLongFileName.go
|- veryLongFileName_test.go
|- veryLongFileName_amd64_windows.go
英文:

Long all small caps for filenames is a terrible idea!

Filenames do not have semantic meaning at go - they are not imported or referenced. Code files in go are compiled to the package they are assigned to as one unit.

Since the article in effective go don't include limitations on muti word filenames except underscore (for build, ignore, test and compile purposes) mixedCaps are the best option for multi word filenames convention. And it's recommend by the same article "effective go"

Tl;dr:

- smallallcupspackagename
|- veryLongFileName.go
|- veryLongFileName_test.go
|- veryLongFileName_amd64_windows.go

答案6

得分: 1

除了其他建议之外,命名约定应包括(.)。这意味着如果你有一个名为"User"的模块,你正在使用路由,并且模型被定义在用户模块内。它们的名称应该像user.models.go、user.route.go、user.service.go这样。这样做更清晰、更易读。

英文:

apart from all the other suggestions, the naming conventions should include (.). what it means is that if you have a module as "User", you are using routes, and models are defined inside the user module. Their name should be like user.models.go, user.route.go, user.service.go.
This is just clearer and easier to read.

huangapple
  • 本文由 发表于 2014年8月6日 21:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/25161774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定