Getting Started with Go: A Beginner's Guide
Learn the basics of Go programming language — installation, syntax, types, and your first program.
Why Go?
Go (Golang) is a statically typed, compiled language designed at Google. It is known for simplicity, fast compilation, built-in concurrency, and excellent standard library.
Key strengths: - Simple and easy to learn - Fast compilation and execution - Built-in concurrency (goroutines and channels) - Excellent tooling (formatting, testing, documentation) - Strong standard library
Installation
Download Go from [go.dev](https://go.dev/dl/). After installation, verify:
``go
// Check version
// $ go version
// go version go1.22.0 darwin/arm64
`
Your First Go Program
`go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
`
Run it with: go run main.go
Variables and Types
`go
package main
import "fmt"
func main() { // Explicit type declaration var name string = "Alice" var age int = 30 var height float64 = 5.7 var isActive bool = true
// Short variable declaration (type inferred) city := "Seoul" score := 95
// Constants const pi = 3.14159 const maxRetries = 3
fmt.Println(name, age, height, isActive, city, score, pi, maxRetries)
}
`
Basic Types
| Type | Description | Example |
|------|-------------|---------|
| int | Integer | 42 |
| float64 | Floating point | 3.14 |
| string | Text | "hello" |
| bool | Boolean | true |
| byte | Alias for uint8 | 'A' |
| rune | Alias for int32 (Unicode) | '\u4e16' |
Control Flow
`go
// If-else
if score >= 90 {
fmt.Println("A")
} else if score >= 80 {
fmt.Println("B")
} else {
fmt.Println("C")
}
// For loop (Go's only loop) for i := 0; i < 5; i++ { fmt.Println(i) }
// While-style loop count := 0 for count < 10 { count++ }
// Range loop fruits := []string{"apple", "banana", "cherry"} for index, fruit := range fruits { fmt.Printf("%d: %s\n", index, fruit) }
// Switch
switch day := "Monday"; day {
case "Monday":
fmt.Println("Start of week")
case "Friday":
fmt.Println("Almost weekend")
default:
fmt.Println("Regular day")
}
`
Functions
`go
// Basic function
func add(a, b int) int {
return a + b
}
// Multiple return values func divide(a, b float64) (float64, error) { if b == 0 { return 0, fmt.Errorf("division by zero") } return a / b, nil }
// Named return values func swap(a, b string) (first, second string) { first = b second = a return }
// Variadic function
func sum(numbers ...int) int {
total := 0
for _, n := range numbers {
total += n
}
return total
}
`
Structs
`go
type User struct {
Name string
Email string
Age int
}
// Method on struct func (u User) Greet() string { return fmt.Sprintf("Hello, I'm %s", u.Name) }
func main() {
user := User{Name: "Alice", Email: "alice@example.com", Age: 28}
fmt.Println(user.Greet())
}
`
Error Handling
Go uses explicit error handling instead of exceptions:
`go
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
``
Next Steps
1. Learn about slices and maps for data structures 2. Explore goroutines and channels for concurrency 3. Build a simple CLI tool or web server 4. Read the [Tour of Go](https://go.dev/tour/)
Go's simplicity is its greatest strength. Start building projects early and let the language's design guide you toward clean, efficient code.