# Effective Go

## Metadata
- Author: [[go.dev]]
- Full Title: Effective Go
- Category: #articles
- Summary: "Effective Go" provides guidance on writing clear and idiomatic Go code, highlighting its unique features compared to other programming languages. The document explains concepts like multiple return values, named result parameters, and function declarations, emphasizing Go's simple syntax and safe programming practices. It also covers memory allocation and the use of goroutines for concurrency, showcasing Go's distinct approach to programming.
- URL: https://go.dev/doc/effective_go
## Highlights
- The [Go package sources](https://go.dev/src/) are intended to serve not only as the core library but also as examples of how to use the language. ([View Highlight](https://read.readwise.io/read/01jvtbx40zsmpdxrsze0sjmw3x))
- By convention, packages are given lower case, single-word names; there should be no need for underscores or mixedCaps. ([View Highlight](https://read.readwise.io/read/01jvtcm5jc53e319eb1dyrcmtq))
- By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: `Reader`, `Writer`, `Formatter`, `CloseNotifier` etc. ([View Highlight](https://read.readwise.io/read/01jvtcw9j2cmvhxmv7rh7d2yn2))
- Finally, the convention in Go is to use `MixedCaps` or `mixedCaps` rather than underscores to write multiword names. ([View Highlight](https://read.readwise.io/read/01jvtcyabz6gdyh39a26p43kyh))
- Like C, Go's formal grammar uses semicolons to terminate statements, but unlike in C, those semicolons do not appear in the source. Instead the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them. ([View Highlight](https://read.readwise.io/read/01jvtczh29dq5mpzswx6jz04fy))
- f, err := os.Open(name)
if err != nil {
return err
}
codeUsing(f)
This is an example of a common situation where code must guard against a sequence of error conditions. The code reads well if the successful flow of control runs down the page, eliminating error cases as they arise. Since error cases tend to end in `return` statements, the resulting code needs no `else` statements. ([View Highlight](https://read.readwise.io/read/01jvtd4wc948ghpqp81vc31pf1))
- In a `:=` declaration a variable `v` may appear even if it has already been declared, provided:
• this declaration is in the same scope as the existing declaration of `v` (if `v` is already declared in an outer scope, the declaration will create a new variable §),
• the corresponding value in the initialization is assignable to `v`, and
• there is at least one other variable that is created by the declaration. ([View Highlight](https://read.readwise.io/read/01jvtd6erf25mt6a432zrn1jsr))
- For[¶](https://go.dev/doc/effective_go/#for)
The Go `for` loop is similar to—but not the same as—C's. It unifies `for` and `while` and there is no `do-while`. There are three forms, only one of which has semicolons.
// Like a C for
for init; condition; post { }
// Like a C while
for condition { }
// Like a C for(;;)
for { } ([View Highlight](https://read.readwise.io/read/01jvtd9yemyyz3td64y57yk0q4))