Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 5, 2019 17:04
Show Gist options
  • Save betandr/8d08d642ca380ba95be250c09a787fbc to your computer and use it in GitHub Desktop.
Save betandr/8d08d642ca380ba95be250c09a787fbc to your computer and use it in GitHub Desktop.
Pass by reference and value
package main
import "fmt"
type T struct{ X int }
// mutable
func inc1(t *T) {
t.X++
}
// immutable
func inc2(t T) T {
return T{t.X + 1}
}
func main() {
var a = new(T)
var b = T{X: 0}
inc1(a)
b = inc2(b)
fmt.Println(a.X)
fmt.Println(b.X)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment