Created
September 26, 2016 17:23
-
-
Save ischneider/0f0e3bf1a0adf324d9be812dc0d8dbaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
) | |
type D struct { | |
v string | |
} | |
func s(o interface{}) { | |
fmt.Fprintf(os.Stdout, "%#v %p\n", o, o) | |
} | |
// value in, value out | |
func alter(d D) D { | |
d.v += "-altered" | |
return d | |
} | |
// pointer in, pointer out | |
func alterp(d *D) *D { | |
d.v += "-altered" | |
return d | |
} | |
// pointer in, value out | |
func alterprv(d *D) D { | |
d.v += "-altered" | |
return *d | |
} | |
func main() { | |
d := D{"1"} | |
s(alter(d)) | |
s(d) | |
s(alterp(&d)) | |
s(d) | |
s(alterprv(&d)) | |
s(d) | |
dp := &D{"2"} | |
s(alter(*dp)) | |
s(dp) | |
s(alterp(dp)) | |
s(dp) | |
s(alterprv(dp)) | |
s(dp) | |
dc := *dp | |
s(alterp(&dc)) | |
s(dc) | |
s(dp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment