+0
日增长
8,842
stars
1,106
forks
200
watchers
56
prs
27
issues
0
releases
README
Go Cheat Sheet
Index
1. Basic Syntax
2. Operators
Arithmetic
Comparison
Logical
Other
3. Declarations
4. Functions
Functions as values and closures
Variadic Functions
5. Built-in Types
6. Type Conversions
7. Packages
8. Control structures
If
Loops
Switch
9. Arrays, Slices, Ranges
Arrays
Slices
Operations on Arrays and Slices
10. Maps
11. Structs
12. Pointers
13. Interfaces
14. Embedding
15. Errors
16. Concurrency
Goroutines
Channels
Channel Axioms
17. Printing
18. Reflection
Type Switch
Examples
19. Snippets
Files Embedding
HTTP Server
Credits
Most example code taken from A Tour of Go, which is an excellent introduction to Go.
If you're new to Go, do that tour. Seriously.
Go in a Nutshell
Imperative language
Statically typed
Syntax tokens similar to C (but less parentheses and no semicolons) and the structure to Oberon-2
Compiles to native code (no JVM)
No classes, but structs with methods
Interfaces
No implementation inheritance. There's type embedding, though.
Functions are first class citizens
Functions can return multiple values
Has closures
Pointers, but not pointer arithmetic
Built-in concurrency primitives: Goroutines and Channels
Basic Syntax
Hello World
File hello.go:
go
package main
import "fmt"
func main() {
fmt.Println("Hello Go")
}
$ go run hello.go
Operators
Arithmetic
|Operator|Description|
|--------|-----------|
|+|addition|
|-|subtraction|
||multiplication|
|/|quotient|
|%|remainder|
|&|bitwise and|
|\||bitwise or|
|^|bitwise xor|
|&^|bit clear (and not)|
||right shift|
Comparison
|Operator|Description|
|--------|-----------|
|==|equal|
|!=|not equal|
||greater than|
|=|greater than or equal|
Logical
|Operator|Description|
|--------|-----------|
|&&|logical and|
|\|\||logical or|
|!|logical not|
Other
|Operator|Description|
|--------|-----------|
|&|address of / create pointer|
||dereference pointer|
| package rand)
Upper case identifier: exported (visible from other packages)
Lower case identifier: private (not visible from other packages)
Control structures
If
go
func main() {
// Basic one
if x 10 {
return x
} else if x == 10 {
return 10
} else {
return -x
}
// You can put one statement before the condition
if a := b + c; a 42:
fmt.Println("Greater")
}
// cases can be presented in comma-separated lists
var char byte = '?'
switch char {
case ' ', '?', '&', '=', '', '+', '%':
fmt.Println("Should escape")
}
Arrays, Slices, Ranges
Arrays
go
var a [10]int // declare an int array with length 10. Array length is part of the type!
a[3] = 42 // set elements
i := a[3] // read elements
// declare and initialize
var a = [2]int{1, 2}
a := [2]int{1, 2} //shorthand
a := [...]int{1, 2} // elipsis - Compiler figures out array length
Slices
go
var a []int // declare a slice - similar to an arraTop issues
#51Page numbers and release version of cheat-sheet4 comments · 2018-09-09#78Update your golang_refcard.pdf, please1 comments · 2022-10-22#69Generic type in Golang 1.181 comments · 2022-03-29#19Functions As Values And Closures gives out whole bunch of errors1 comments · 2015-04-19#13All non-built in types can have methods1 comments · 2015-03-13#84ESP translation0 comments · 2024-02-26#82add testing to the cheat-sheet0 comments · 2024-01-09#73Adding common Go packages?0 comments · 2022-04-17