Multiple Return Values
Go functions can return multiple values. This is frequently used to return both a result and an error from a single function call.
Open snippet →Read these Beginner Go snippets line by line — each one comes with a written breakdown of what the code does and why.
Go functions can return multiple values. This is frequently used to return both a result and an error from a single function call.
Open snippet →In Go, you can declare variables using the 'var' keyword with an explicit type, or the short declaration operator ':=' which infers the type automatically.
Open snippet →Slices are dynamic windows into arrays. Unlike arrays, slices can grow in size using the built-in 'append' function.
Open snippet →Go only has one looping construct: 'for'. It can be used as a standard C-style loop or as a 'while' loop by omitting the initialization and post statements.
Instead of using exceptions, Go uses explicit error checking. Functions return an 'error' type as their last return value, which should be checked immediately.
Open snippet →Arrays in Go have a fixed length that is part of their type. Once defined, their size cannot change.
Open snippet →The if statement evaluates a boolean condition. Parentheses are not used around the condition, but curly braces for the block are mandatory.
Open snippet →Structs are used to group related data together. Unlike classes in other languages, Go uses structs to define the shape of data objects.
Open snippet →Maps are Go's built-in associative data type (hash tables). they store data in key-value pairs.
Open snippet →A method is a function with a special receiver argument. It allows you to attach behavior to structs.
Open snippet →Go allows two ways to declare variables. The `var` keyword with explicit type, or the `:=` short syntax which infers the type from the value. Variables declared with `:=` inside a function are call…
Open snippet →Slices are dynamic, growable sequences in Go. Unlike arrays, slices can change size. The `append` function adds elements and automatically grows the underlying array when needed. `len()` returns th…
Open snippet →Go uses multiple return values for error handling, a core design pattern. Functions return both a value and an error. The error is nil on success, otherwise it contains the error message. The `if e…
Open snippet →Go has only one loop keyword: `for`. There is no `while` keyword. The `for` loop without initialization and post statements acts as a while loop. This makes Go simpler while remaining powerful.
Open snippet →The `range` keyword iterates over slices and maps. It returns two values: index and element. The blank identifier `_` is used when you don't need a value, telling Go to discard it.
Open snippet →Maps in Go are unordered key-value data structures. They are created using the built-in `map` keyword with syntax `map[KeyType]ValueType`. You can add entries using bracket notation, check for exis…
Open snippet →Structs are composite types that group together related fields. You define a struct using the `type` keyword, then create instances with brace initialization. Methods can be attached to structs usi…
Open snippet →Pointers hold memory addresses instead of values. The `&` operator gets a pointer to a variable, while `*` dereferences a pointer to access the value. In `double`, the `*int` parameter expects a po…
Open snippet →Go's switch is more flexible than in many languages. It can compare against values or conditions without an expression. Cases don't fall through by default (no break needed), but you can use `fallt…
Open snippet →The `defer` keyword schedules a function call to execute when the surrounding function returns. This is essential for cleanup tasks like closing files or releasing resources. Deferred functions exe…
Open snippet →The `iota` keyword generates consecutive untyped integer constants. In the first block, it creates color codes (1, 2, 3). In the second block, `1 << iota` creates bit flags (1, 2, 4). The underscor…
Open snippet →Variadic functions accept zero or more arguments using the `...T` syntax. Inside the function, the variable becomes a slice. You can also have required parameters before the variadic part. This pat…
Open snippet →Go strings are byte arrays, not rune arrays. `len(s)` returns byte count, which differs for Unicode. Use `len([]rune(s))` for actual character count. Slice bytes directly with `s[:5]` but use runes…
Open snippet →Methods can be defined with value receivers `(*Counter)` or pointer receivers. Pointer receivers modify the original struct, while value receivers work on a copy. For mutable operations, always use…
Open snippet →Empty interface `interface{}` accepts any type. Type assertions extract the underlying type with `i.(type)` in switch or `i.(T)` for direct assertion. The comma-ok idiom checks if assertion succeed…
Open snippet →Go uses packages to organize code into modular units. The import statement brings in external packages from the standard library or third-party sources. Packages like fmt for formatted I/O, math fo…
Open snippet →Go is a statically typed language with explicit type declarations. The basic types include int for integers, float64 for decimal numbers, bool for true/false values, and string for text. Go does no…
Open snippet →If statements in Go evaluate boolean conditions and execute code blocks accordingly. Unlike many languages, Go does not use parentheses around conditions. The else if chain allows for multiple cond…
Open snippet →Goroutines are lightweight threads managed by the Go runtime, enabling concurrent execution. Simply prefix a function call with 'go' to run it concurrently. Unlike parallel processing (using multip…
Open snippet →Go has built-in testing support through the testing package. Test files are named with _test.go suffix. Test functions start with Test prefix and receive a *testing.T argument. The t.Errorf() metho…
Open snippet →