By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A Go function should fit on a screen and do one thing. The compiler does not care about length, but reviewers do: when a function parses input, applies policy and writes output, the test for it has to set up all three. Split it and each piece becomes trivially callable. Early returns help here — guard clauses at the top keep the happy path unindented.
Functions are full values in Go. You can store them in variables, pass them to other functions, return them from functions and put them in struct fields. That is what makes http.HandlerFunc, slices.SortFunc and middleware chains possible without a framework.
The convention is (result, error): the value first, the error second, and the caller checks it immediately. An error is just a value — no exception unwinds the stack — so an intermediate function that does not know what to do with a failure returns it upward, optionally adding context.
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
type event struct {
Service string
Level string
Status int
}
var errBadLine = errors.New("bad event line")
func parseEvent(line string) (event, error) {
parts := strings.Split(line, "|")
if len(parts) != 3 {
return event{}, fmt.Errorf("%w: want 3 fields, got %d", errBadLine, len(parts))
}
status, err := strconv.Atoi(strings.TrimSpace(parts[2]))
if err != nil {
return event{}, fmt.Errorf("%w: status %q", errBadLine, strings.TrimSpace(parts[2]))
}
return event{
Service: strings.TrimSpace(parts[0]),
Level: strings.TrimSpace(parts[1]),
Status: status,
}, nil
}
func parseAll(lines []string) ([]event, []error) {
events := make([]event, 0, len(lines))
failures := make([]error, 0)
for _, line := range lines {
e, err := parseEvent(line)
if err != nil {
failures = append(failures, err)
continue
}
events = append(events, e)
}
return events, failures
}
func counter() func() int {
n := 0
return func() int {
n++
return n
}
}
func main() {
lines := []string{"api|error|500", "worker|info|200", "broken", "api|warn|oops"}
events, failures := parseAll(lines)
next := counter()
for _, e := range events {
fmt.Printf("%d %s %s %d\n", next(), e.Service, e.Level, e.Status)
}
for _, err := range failures {
fmt.Println("skip:", err)
}
}A closure is a function value that keeps referencing variables from its defining scope. counter above returns a function over its own private n; every call sees the latest value. The same mechanism builds request-scoped loggers, retry predicates and http.Handler middleware. Since Go 1.22 each iteration of a for loop has its own variables, which removes the old surprise where every captured loop variable ended up with the final value.
| Shape | Example | Why it matters |
|---|---|---|
| Single result | func now() time.Time | No failure path to report |
| Result and error | func parse(line string) (event, error) | The caller decides what a failure means |
| Many values | func split(hostport string) (host string, port int) | Return data, not one packed struct |
| Function value | var filter func(event) bool | Behavior becomes a parameter |
| Closure | next := counter() | State without a struct or a global |
Key takeaways
Keep functions small and single-purpose; guard clauses keep the happy path unindented.
Functions are values in Go, which is what makes handlers and middleware composable.
The idiomatic failure shape is (result, error) with the error checked immediately.
Closures capture variables, not snapshots; Go 1.22+ gives each loop iteration its own variables.
A function that cannot handle an error returns it upward, adding context instead of swallowing it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.