var x int = 10 Variable with explicit type
x := 10 Short variable declaration (inferred)
const Pi = 3.14159 Constant declaration
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 Integer types
float32 float64 Floating point types
string bool byte rune Other basic types
if x > 0 {
...
} else if x == 0 {
...
} else {
...
} If/else (no parentheses needed)
for i := 0; i < 10; i++ { ... } Classic for loop
for _, v := range slice { ... } Range over slice/map/string
for condition { ... } While-style loop
for { ... } Infinite loop
switch x {
case 1:
...
case 2, 3:
...
default:
...
} Switch (no fallthrough by default)
fmt.Println("hello") Print with newline
fmt.Sprintf("name: %s, age: %d", name, age) Format string
strconv.Atoi(s) / strconv.Itoa(n) String <-> int conversion
s := []int{1, 2, 3} Create a slice literal
s := make([]int, 0, 10) Create slice with length and capacity
s = append(s, 4, 5) Append elements to slice
s[1:3] Slice of slice (index 1 to 2)
len(s) / cap(s) Length and capacity of slice
copy(dst, src) Copy elements between slices
s = append(s[:i], s[i+1:]...) Delete element at index i
sort.Ints(s) / sort.Strings(s) Sort slice in-place
slices.Contains(s, val) Check if slice contains value (Go 1.21+)
m := map[string]int{"a": 1} Create a map literal
m := make(map[string]int) Create empty map
m["key"] = value Set map value
val, ok := m["key"] Get value with existence check
delete(m, "key") Delete key from map
for k, v := range m { ... } Iterate over map entries
func add(a, b int) int {
return a + b
} Function with return type
func divide(a, b float64) (float64, error) Multiple return values
func greet(name string, opts ...string) Variadic function
result, err := divide(10, 3) Capture multiple returns
fn := func(x int) int { return x * 2 } Anonymous function / closure
defer file.Close() Defer execution until function returns
func init() { ... } Package initialization function
type User struct {
Name string
Age int
} Struct type definition
u := User{Name: "Alice", Age: 30} Struct literal
u := &User{Name: "Alice"} Pointer to struct
func (u User) Greet() string {
return "Hi " + u.Name
} Method on struct (value receiver)
func (u *User) SetName(n string) {
u.Name = n
} Method with pointer receiver (mutate)
type Admin struct {
User
Role string
} Struct embedding (composition)
type Config struct {
Host string `json:"host"`
} Struct tags (JSON, DB, etc.)
type Reader interface {
Read(p []byte) (n int, err error)
} Interface definition
type Stringer interface {
String() string
} Stringer interface (like toString)
interface{} / any Empty interface (accepts any type)
val, ok := i.(ConcreteType) Type assertion with check
switch v := i.(type) {
case string:
...
case int:
...
} Type switch
go func() { ... }() Launch goroutine
ch := make(chan int) Create unbuffered channel
ch := make(chan int, 10) Create buffered channel (cap 10)
ch <- value Send value to channel
value := <-ch Receive value from channel
close(ch) Close channel (no more sends)
select {
case v := <-ch1:
...
case ch2 <- val:
...
default:
...
} Select on multiple channels
var mu sync.Mutex
mu.Lock()
defer mu.Unlock() Mutex for shared state
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
...
}()
wg.Wait() WaitGroup for goroutine coordination
if err != nil {
return fmt.Errorf("failed: %w", err)
} Wrap and propagate error
errors.New("something failed") Create simple error
fmt.Errorf("user %d: %w", id, err) Create formatted error (wrapping)
errors.Is(err, target) Check if error matches target
errors.As(err, &target) Extract typed error from chain
type MyError struct {
Code int
Msg string
}
func (e *MyError) Error() string {
return e.Msg
} Custom error type
panic("fatal") / recover() Panic and recover (rare, avoid)
Free Online Go Cheatsheet
A searchable Go (Golang) quick reference covering basics, slices, maps, functions, structs, interfaces, goroutines, channels, and error handling. Copy any snippet with one click.
About this cheatsheet
A comprehensive Go cheatsheet covering the language's core features and concurrency model.
- 60+ practical Go snippets
- 7 categories from basics to concurrency
- Goroutines and channels explained
- Error handling patterns
- One-click copy to clipboard
- Struct methods and interfaces covered
100% free. No signup required. No data collected or sent anywhere.