Go Style: Difference between revisions
(→Naming) |
|||
Line 35: | Line 35: | ||
* [[Go_Structs#Idiomatic_Struct_Naming_Conventions|Idiomatic Struct Naming Conventions]] | * [[Go_Structs#Idiomatic_Struct_Naming_Conventions|Idiomatic Struct Naming Conventions]] | ||
* [[Go_Interfaces#Idiomatic_Interface_Conventions|Idiomatic Interface Conventions]] | * [[Go_Interfaces#Idiomatic_Interface_Conventions|Idiomatic Interface Conventions]] | ||
* [[Go_Language_Error_Handling#idiom|Idiomatic Error Conventions]] | |||
<font color=darkkhaki>Integrate this: | <font color=darkkhaki>Integrate this: | ||
* https://go.dev/talks/2014/names.slide#1 | * https://go.dev/talks/2014/names.slide#1 |
Revision as of 02:40, 28 December 2023
External
- https://google.github.io/styleguide/go/
- https://go.dev/doc/effective_go
- https://github.com/golang/go/wiki/CodeReviewComments
Internal
Overview
The Go standard library is a good source of code examples, comments and style.
Naming
Naming is one of the most important aspects of Go development. Writing idiomatic Go requires understanding of its core naming principles.
Identifiers should use camel case: SomeColor
or someColor
, depending on whether they are visible outside the package or not. This is how package encapsulation works.
Do not use snake case some_color
.
Every exported name should be documented with a comment, following idiomatic comment conventions.
Acronyms should have a consistent case. URL
or url
is correct, Url
is not.
When a variable, struct or interface is imported from another package, its name includes the package name or alias: mypackage.MyVar
.
Use name abbreviations only if they are widely used (example: fmt
).
Avoid name collisions, when possible. If you introduce a set of string functions, avoid calling the package strings
because a package with the same name exists in the standard library already.
Other naming conventions:
- Source Files Naming Conventions
- Idiomatic Package Conventions
- Idiomatic Variable Naming Conventions
- Idiomatic Function Naming Conventions
- Idiomatic Struct Naming Conventions
- Idiomatic Interface Conventions
- Idiomatic Error Conventions
Integrate this:
- https://go.dev/talks/2014/names.slide#1
- https://golang.org/doc/effective_go.html#names
- https://blog.golang.org/package-names
- https://rakyll.org/style-packages/
Comments
Idiomatic Error Handling
Getters and Setters
It is neither idiomatic, not necessary to put "Get" in the getter's name. If you have a field called owner (lower case, unexported), the getter method should be called Owner()
(upper case, exported), not Owner()
.
A setter function, if needed, should be called SetOwner()
.