// Chapter 21 example: a variable inside a function starts fresh on every // call, and a variable outside any function stays between calls. // Run with: go run exercises/21/03-counter-scope.go package main import "fmt" // steps is outside every function, so all of them can see it. Outside a // function you must write var, because := only works inside one. var steps = 0 func counterStep() int { count := 0 count = count + 1 return count } func step() int { steps = steps + 1 return steps } func main() { fmt.Println(counterStep()) // 1 fmt.Println(counterStep()) // 1 fmt.Println(counterStep()) // 1 fmt.Println(step()) // 1 fmt.Println(step()) // 2 fmt.Println(step()) // 3 }