// A function parameter is MUTABLE: a callee may write through a parameter // pointer and may assign to a parameter, and the caller sees the write. Named // results are required and are assignable like locals. // // Pinned because the memory model claimed the opposite for a long time // ("parameters are immutable borrows, writing through a parameter pointer is a // compile-time error") while neither compiler enforced it and 570 sites in the // tree already relied on mutability. Round 296 made the model say what the // compilers do. package main type pair struct { a int32 b int32 } func fill(p *pair, n int32) (seen int32) { p.a = n p.b = p.a * 2 seen = p.a + p.b return } func bump(n int32) (out int32) { n = n + 1 out = n * 10 return } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } var p pair seen := fill(&p, 7) if p.a != 7 { println("write through parameter lost: p.a =", p.a.String()) return } if p.b != 14 { println("write through parameter lost: p.b =", p.b.String()) return } if seen != 21 { println("named result wrong:", seen.String()) return } if bump(4) != 50 { println("assign to parameter lost:", bump(4).String()) return } println("param mutable ok") }