Golang Cheat Sheet#
1. Basic Syntax#
// Single-line comment
/*
Multi-line
comment
*/
Variables and Data Types#
var x int = 5
y := 10 // Type inference
// Basic types
var (
a bool = true
b int = 10
c float64 = 3.14
d string = "Hello"
)
Constants#
const Pi = 3.14
const (
StatusOK = 200
StatusNotFound = 404
)
Operators#
// Arithmetic: +, -, *, /, %, ++, --
// Comparison: ==, !=, <, >, <=, >=
// Logical: &&, ||, !
// Bitwise: &, |, ^, <<, >>
Type Casting#
i := 42
f := float64(i)
s := string(i)
2. Control Structures#
Conditional Statements#
if x > 0 {
// code
} else if x < 0 {
// code
} else {
// code
}
switch day {
case "monday":
// code
case "tuesday", "wednesday":
// code
default:
// code
}
Loops#
for i := 0; i < 10; i++ {
// code
}
for condition {
// while loop equivalent
}
for {
// infinite loop
}
for index, value := range collection {
// range loop
}
Break and Continue Statements#
for {
if condition {
break
}
if otherCondition {
continue
}
}
3. Functions#
Function Declaration and Definition#
func greet(name string) string {
return "Hello, " + name
}
Parameters and Return Values#
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
Anonymous Functions / Lambdas#
func() {
fmt.Println("Anonymous function")
}()
add := func(a, b int) int {
return a + b
}
Scope and Closures#
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
4. Data Structures#
Arrays/Slices#
var arr [5]int
slice := []int{1, 2, 3, 4, 5}
slice = append(slice, 6)
Maps#
m := make(map[string]int)
m["key"] = 42
value, exists := m["key"]
Sets#
// Go doesn't have a built-in set type
// Use a map[Type]bool instead
set := make(map[string]bool)
set["item"] = true
5. Object-Oriented Programming#
Structs and Methods#
type Rectangle struct {
width, height float64
}
func (r Rectangle) Area() float64 {
return r.width * r.height
}
Interfaces#
type Shape interface {
Area() float64
}
6. Error Handling#
if err != nil {
log.Fatal(err)
}
// Custom errors
type MyError struct {
message string
}
func (e *MyError) Error() string {
return e.message
}
7. File I/O#
// Reading
data, err := ioutil.ReadFile("file.txt")
// Writing
err := ioutil.WriteFile("file.txt", data, 0644)
// Working with directories
files, err := ioutil.ReadDir(".")
8. Modules and Packages#
import (
"fmt"
"math"
)
// Creating modules
// In go.mod file:
// module example.com/mymodule
9. Standard Library#
- fmt: Formatted I/O
- os: Operating system functionality
- io: Basic I/O interfaces
- net/http: HTTP client and server implementations
- encoding/json: JSON encoding and decoding
- time: Time and duration functions
10. Concurrency#
// Goroutines
go function()
// Channels
ch := make(chan int)
ch <- 42 // Send
value := <-ch // Receive
// Select
select {
case msg1 := <-ch1:
// Use msg1
case msg2 := <-ch2:
// Use msg2
default:
// Run if no channel is ready
}
11. Memory Management#
- Go uses automatic garbage collection
defer
keyword for cleanup operations
12. Important Language-Specific Features#
- Goroutines and Channels
- Defer statements
- Panic and Recover
- go generate
13. Best Practices and Style Guide#
- Use gofmt for standard formatting
- Follow naming conventions (camelCase for unexported, PascalCase for exported)
- Prefer composition over inheritance
- Handle errors explicitly
14. Useful Resources#