Last active
December 24, 2019 20:03
-
-
Save mtilson/0418bb93fcc345b64de6440dbcf2fc3a to your computer and use it in GitHub Desktop.
how to use pointers to share elements between slices and maps [golang]
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" | |
// Post is the simplest blog post object with ID, count of references, and page slug | |
type Post struct { | |
ID string | |
RefCount int | |
Slug string | |
} | |
func main() { | |
post100 := Post{"100-aaaaaaaa", 100, "golang-do-not-use-net-http-default-mux"} | |
post200 := Post{"200-bbbbbbbb", 200, "golang-use-contexts-for-graceful-shutdow"} | |
post300 := Post{"300-cccccccc", 300, "golang-what-you-did-not-know-on-pointers"} | |
post400 := Post{"400-dddddddd", 400, "golang-middleware-with-net-http-handlefunc"} | |
post500 := Post{"500-eeeeeeee", 500, "golang-middleware-with-net-http-handler-interface"} | |
post600 := Post{"600-ffffffff", 600, "golang-build-information-into-executable"} | |
post700 := Post{"700-gggggggg", 700, "golang-create-grpc-server"} | |
post800 := Post{"800-hhhhhhhh", 800, "golang-readiness-and-liveness-probes"} | |
// map is convenient in cases we need to access elements by knowing field, e.g. ID (give me | |
// details of blog post with ID="100-aaaaaaaa" | |
mapObjects := map[string]Post{post100.ID: post100, post200.ID: post200, post300.ID: post300, post400.ID: post400} | |
mapPointers := map[string]*Post{post500.ID: &post500, post600.ID: &post600, post700.ID: &post700, post800.ID: &post800} | |
// slice is convenient in cases we need to access elements in some order (give me last 10 blog posts, ...) | |
sliceObjects := []Post{post100, post200, post300, post400} | |
slicePointers := []*Post{&post500, &post600, &post700, &post800} | |
postA := mapObjects["100-aaaaaaaa"] | |
postA.RefCount++ // doesn't change stored object | |
postB := mapObjects["200-bbbbbbbb"] | |
postB.RefCount++ // doesn't change stored object | |
postE := mapPointers["500-eeeeeeee"] | |
postE.RefCount++ | |
postF := mapPointers["600-ffffffff"] | |
postF.RefCount++ | |
post3 := sliceObjects[2] | |
post3.RefCount++ // doesn't change stored object | |
post4 := sliceObjects[3] | |
post4.RefCount++ // doesn't change stored object | |
post7 := slicePointers[2] | |
post7.RefCount++ | |
post8 := slicePointers[3] | |
post8.RefCount++ | |
fmt.Println("Elements of object's map:") | |
for _, v := range mapObjects { | |
fmt.Printf("\t%v\n", v) | |
} | |
fmt.Println("Elements of pointer's map with RefCount's incremented:") | |
for _, v := range mapPointers { | |
fmt.Printf("\t%v\n", v) | |
} | |
fmt.Println("Elements of object's slice:") | |
for _, v := range sliceObjects { | |
fmt.Printf("\t%v\n", v) | |
} | |
fmt.Println("Elements of pointer's slice with RefCount's incremented:") | |
for _, v := range slicePointers { | |
fmt.Printf("\t%v\n", v) | |
} | |
} |
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
$ go run main.go | |
Elements of object's map: | |
{100-aaaaaaaa 100 golang-do-not-use-net-http-default-mux} | |
{200-bbbbbbbb 200 golang-use-contexts-for-graceful-shutdow} | |
{300-cccccccc 300 golang-what-you-did-not-know-on-pointers} | |
{400-dddddddd 400 golang-middleware-with-net-http-handlefunc} | |
Elements of pointer's map with RefCount's incremented: | |
&{500-eeeeeeee 501 golang-middleware-with-net-http-handler-interface} | |
&{600-ffffffff 601 golang-build-information-into-executable} | |
&{700-gggggggg 701 golang-create-grpc-server} | |
&{800-hhhhhhhh 801 golang-readiness-and-liveness-probes} | |
Elements of object's slice: | |
{100-aaaaaaaa 100 golang-do-not-use-net-http-default-mux} | |
{200-bbbbbbbb 200 golang-use-contexts-for-graceful-shutdow} | |
{300-cccccccc 300 golang-what-you-did-not-know-on-pointers} | |
{400-dddddddd 400 golang-middleware-with-net-http-handlefunc} | |
Elements of pointer's slice with RefCount's incremented: | |
&{500-eeeeeeee 501 golang-middleware-with-net-http-handler-interface} | |
&{600-ffffffff 601 golang-build-information-into-executable} | |
&{700-gggggggg 701 golang-create-grpc-server} | |
&{800-hhhhhhhh 801 golang-readiness-and-liveness-probes} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment