Created
May 7, 2018 12:52
-
-
Save marclambrichs/59899a38a9cb31fc60389a261c359f16 to your computer and use it in GitHub Desktop.
using the & operator
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
// -*- mode:go;mode:go-playground -*- | |
// snippet of code @ 2018-05-07 14:35:38 | |
// === Go Playground === | |
// Execute the snippet with Ctl-Return | |
// Remove the snippet completely with its dir and all files M-x `go-playground-rm` | |
package main | |
import ( | |
"fmt" | |
) | |
type someType struct { | |
ID int64 | |
Name string | |
} | |
func main() { | |
// Create a pointer p, of type *someType, which points to an unnamed someType variable | |
v := someType{ID: 1, Name: "name1"} | |
p1 := &v.Name | |
fmt.Printf("name = %s\n", v.Name) | |
*p1 = "name2" | |
fmt.Printf("name = %s\n", v.Name) | |
p2 := &(v.Name) | |
fmt.Printf("name = %s\n", v.Name) | |
*p2 = "name3" | |
fmt.Printf("name = %s\n", v.Name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment