Go favors small, explicit programs built from packages, functions, values and error returns. Its syntax is deliberately compact, but a beginner still needs to understand how modules, names, types, loops and error handling fit together. These examples use the standard library and can be compiled with a currently supported Go toolchain.
Create a module and run one program
mkdir go-first-steps
cd go-first-steps
go mod init example/go-first-steps
# Save the following source as main.go
go run .
package main
import "fmt"
func main() {
fmt.Println("Hello, Go")
}
package main defines an executable package and main() is its entry point. The import statement brings in the standard fmt package. Use go run . to compile and run the current package during development and go build . to produce an executable. Module setup makes dependency choices explicit rather than relying on an unmanaged directory structure.
Values, types and the short-declaration form
package main
import "fmt"
func main() {
name := "Maya"
count := 3
var active bool = true
fmt.Printf("%s has %d tasks (active=%t)\n", name, count, active)
}
The := form declares and initializes a variable inside a function. Go infers its type from the right-hand side; it does not make the variable dynamically typed. Use var when you need an explicit type or declaration without an initializer. Go intentionally rejects unused local variables and unused imports, catching many editing leftovers early.
Functions can return both a value and an error
package main
import (
"errors"
"fmt"
)
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
value, err := divide(12, 4)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(value) // 3
}
Unlike exceptions used by some other languages, Go commonly returns errors as values. Check err explicitly at the call site. The error return documents a failure path and prevents the caller from silently treating the zero result as valid. For network or file operations, return or wrap errors with context that helps diagnose the failed operation; do not expose secrets or internal state in end-user messages.
Use slices and maps for basic collections
package main
import "fmt"
func main() {
numbers := []int{2, 4, 6}
total := 0
for _, n := range numbers {
total += n
}
scores := map[string]int{"A": 8, "B": 5}
fmt.Println(total, scores["A"]) // 12 8
}
A slice is a view over a backing array that can grow when appended; a map associates keys with values. Reading a missing map key yields the element type’s zero value, so use the two-value form value, ok := scores[key] when absence must be distinguished from a genuine zero. Map iteration order is not specified; sort keys explicitly when deterministic output matters.
Formatting, testing and common beginner mistakes
- Run
gofmt -w .to apply Go’s standard formatting to files in a package. - Use
go test ./...as the project grows; keep tests adjacent to the package in_test.gofiles. - Do not ignore returned errors from file, network or parsing operations.
- Do not assume all text is one byte per character: Go strings are byte sequences, and iterating with
rangedecodes UTF-8 code points.
The official Go getting-started tutorial provides the canonical installation, module and execution path. Once these basics are comfortable, learn interfaces, context cancellation, testing, race detection and concurrency through small programs where the expected behavior can be checked.