Go/Naming Conventions: Difference between revisions
From charlesreid1
(Created page with "Go Naming Conventions: smartystreets.com/blog/2018/10/go-naming-tutorial Use single-character receiver names: <pre> package main import "fmt" func main() { new(Printer).P...") |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
Go Naming Conventions: smartystreets.com/blog/2018/10/go-naming-tutorial | Go Naming Conventions: https://smartystreets.com/blog/2018/10/go-naming-tutorial | ||
Use single-character receiver names | ===Note=== | ||
Exported vs Unexported names | |||
* Anything (variable, type, or function) that starts with a capital letter is exported, and visible outside the package. | |||
* Anything that does not start with a capital letter is not exported, and is visible only inside the same package. | |||
===Use single-character receiver names=== | |||
<pre> | <pre> | ||
| Line 20: | Line 29: | ||
} | } | ||
</pre> | </pre> | ||
===Use single-letter variable names=== | |||
<pre> | |||
package main | |||
import "fmt" | |||
func main() { | |||
new(Printer).Print("Use single-letter variables whenever possible") | |||
} | |||
type Printer struct{} | |||
func (P *Printer) Print(M string) (N int, E error) { | |||
return fmt.Println(M) | |||
} | |||
</pre> | |||
=Flags= | |||
{{GoFlag}} | |||
Latest revision as of 05:56, 21 December 2018
Go Naming Conventions: https://smartystreets.com/blog/2018/10/go-naming-tutorial
Note
Exported vs Unexported names
- Anything (variable, type, or function) that starts with a capital letter is exported, and visible outside the package.
- Anything that does not start with a capital letter is not exported, and is visible only inside the same package.
Use single-character receiver names
package main
import "fmt"
func main() {
new(Printer).Print(
"Use only the first letter of a type as the receiver for its methods (oh, wait...), " +
"and (per tip #5) make sure the receiver is exported")
}
type Printer struct{}
func (P *Printer) Print(Message string) (N int, Err error) {
return fmt.Println(Message)
}
Use single-letter variable names
package main
import "fmt"
func main() {
new(Printer).Print("Use single-letter variables whenever possible")
}
type Printer struct{}
func (P *Printer) Print(M string) (N int, E error) {
return fmt.Println(M)
}
Flags
| Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|