Data Types
Go provides a strong, statically typed system with primitive and composite types. Primitive types include numbers, strings, and booleans. Composite types like arrays, slices, maps, and structs help organize and store complex data. Understanding types is key to writing safe and efficient code.
int, float64, string, bool
array, slice, map, struct, pointer, interface
int, float64, string, bool
array, slice, map, struct, pointer, interface
๐ Loops
Go only has the for
loop, but it can replicate while
and infinite loops easily. It's used for iterating over data structures, executing repeated logic, or handling retry conditions. Loop control uses break
, continue
, and labels for advanced flow.
goCopyEditfor i := 0; i < 10
; i++ { ... }for
condition { ... }for { ... } // infinite loop
๐ Conditionals
if
, else if
, and else
in Go are used to control flow based on conditions. These work the same as in other languages, but Go allows short statements before conditions for cleaner code. No parentheses are required around conditions.
goCopyEditif x > 10 { ... } else if x == 10 { ... } else
{ ... }
๐ Switch
Switch is a cleaner alternative to multiple if-else
blocks. It matches values and executes the first matching case
. It automatically breaks after a match (no break
needed), and a default
block handles unmatched cases.
goCopyEditswitch
day {case "Mon"
: ...case "Fri"
: ...default
: ...
}
๐ง Functions
Functions are first-class citizens in Go โ they can be passed around, returned, and stored. They support multiple return values, named results, and closures. Used to encapsulate logic, promote reusability, and keep code organized.
goCopyEditfunc add(a int, b int) int { return
a + b }func multipleReturns() (int, string) { return 1, "ok"
}
๐ฆ Structs
Structs are Goโs way of defining custom data types. They bundle related fields into a single unit and are used to model real-world entities. You can create methods on structs, making them powerful building blocks for larger applications.
goCopyEdittype Person struct { Name string; Age int
}p := Person{Name: "Tom", Age: 30
}
๐ Slices & Maps
Slices are dynamic arrays and are central to most Go programs due to their flexibility. Maps are unordered key-value pairs for fast lookups. Both are built-in and heavily used in data processing, configuration, and more.
goCopyEdits := []int{1, 2, 3
}m := map[string]int{"a": 1, "b": 2
}
๐ช Pointers
Pointers hold memory addresses of variables. Go has safe pointer semantics โ no pointer arithmetic. They are used to share and update data without copying it, especially in function arguments and structs.
goCopyEditx := 10
p := &x*p = 20
๐งฉ Interfaces
Interfaces in Go define behavior, not data. They allow different types to be used interchangeably if they implement the required methods. This enables polymorphism and clean abstraction without inheritance.
goCopyEdittype Shape interface
{ Area() float64
}
๐งต Goroutines (Concurrency)
Goroutines are lightweight threads managed by the Go runtime. Use go
to run a function asynchronously. Great for building high-performance, concurrent systems with minimal effort and memory overhead.
goCopyEditgo func()
{ fmt.Println("Async!"
)
}()
๐งฐ Useful Packages
Goโs standard library is powerful and covers most common tasks. fmt
is for formatting, math
for calculations, strings
for text, and time
for working with clocks and timers. These should be your go-to tools before adding third-party libs.
goCopyEditimport "fmt", "math", "strings", "time"
๐ Pro Tip: Format code using go fmt
, and keep your programs clean, idiomatic, and readable.