← Back to archive

Stdle #109 · defer · 2026-09-23

What does this Go snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

package main
import "fmt"
type S struct{ data []int }
func (s S) Sum() int {
t := 0
for _, v := range s.data {
t += v
}
return t
}
func main() {
s := S{data: []int{1, 2}}
defer fmt.Println(s.Sum())
s.data = append(s.data, 100)
fmt.Println("appended")
}
Defer
2 lines
4 attempts left

Answer & explanation