0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
Go · BeginnerSnippet

Variable Declarations

In Go, you can declare variables using the 'var' keyword with an explicit type, or the short declaration operator ':=' which infers the type automatically.

snippet.go
go
1
2
3
4
5
6
7
8
9
package main
import "fmt"
func main() {
var name string = "Gopher"
age := 10
fmt.Printf("%s is %d years old", name, age)
}
SyntaxData Types
Go · BeginnerSnippet

Working with Slices

Slices are dynamic windows into arrays. Unlike arrays, slices can grow in size using the built-in 'append' function.

snippet.go
go
1
2
3
fruits := []string{"apple", "banana"}
fruits = append(fruits, "cherry")
fmt.Println(fruits[1]) // "banana"
Arrays & ListsData Types
Go · BeginnerSnippet

The For Loop

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.

snippet.go
go
1
2
3
4
5
6
7
8
9
for i := 0; i < 5; i++ {
fmt.Println("Iteration:", i)
}
// While-like loop
sum := 1
for sum < 10 {
sum += sum
}
Control FlowSyntax
Go · BeginnerSnippet

Error Handling Pattern

Instead of using exceptions, Go uses explicit error checking. Functions return an 'error' type as their last return value, which should be checked immediately.

snippet.go
go
1
2
3
4
5
6
result, err := doSomething()
if err != nil {
fmt.Println("Error occurred:", err)
return
}
fmt.Println("Success:", result)
Error HandlingBest Practices
Go · BeginnerSnippet

Basic Arrays

Arrays in Go have a fixed length that is part of their type. Once defined, their size cannot change.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import "fmt"
func main() {
var ids [3]int
ids[0] = 101
ids[1] = 102
ids[2] = 103
names := [2]string{"Alice", "Bob"}
fmt.Println(ids, names)
}
Arrays & ListsData Types
Go · BeginnerSnippet

Conditional Logic with If-Else

The if statement evaluates a boolean condition. Parentheses are not used around the condition, but curly braces for the block are mandatory.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import "fmt"
func main() {
temp := 25
if temp > 30 {
fmt.Println("It is hot")
} else if temp > 20 {
fmt.Println("It is nice")
} else {
fmt.Println("It is cold")
}
}
Control FlowSyntax
Go · BeginnerSnippet

Defining Structs

Structs are used to group related data together. Unlike classes in other languages, Go uses structs to define the shape of data objects.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import "fmt"
type User struct {
Username string
Email string
Active bool
}
func main() {
u := User{Username: "coder123", Email: "[email protected]", Active: true}
fmt.Println(u.Username)
}
Data TypesSyntax
Go · BeginnerSnippet

Working with Maps

Maps are Go's built-in associative data type (hash tables). they store data in key-value pairs.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
import "fmt"
func main() {
menu := map[string]float64{
"Coffee": 2.50,
"Tea": 2.00,
}
menu["Juice"] = 3.25
delete(menu, "Tea")
fmt.Println(menu)
}
Data TypesSyntax