{"id":110,"date":"2020-10-21T13:43:51","date_gmt":"2020-10-21T21:43:51","guid":{"rendered":"http:\/\/iaastalk.com\/?p=110"},"modified":"2020-10-21T13:43:51","modified_gmt":"2020-10-21T21:43:51","slug":"golang-range-iterator-of-pointer","status":"publish","type":"post","link":"http:\/\/iaastalk.com\/?p=110","title":{"rendered":"Golang range iterator  of pointer"},"content":{"rendered":"\n<p>It&#8217;s tricky to using pointer in range iterator.<\/p>\n\n\n\n<p>Please beware variable &#8220;hostStatus&#8221; is a pointer that&#8217;s been reused in each iteration. the location of hostStatus will be the same although it points to a different struct in each iteration.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>It may cause duplicated values in the final results, beware!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n)\n\n\ntype HostStatus struct {\n\tName    string\n\tStatus  string\n\tMessage string\n}\n\nfunc main() {\n\tvalue := &#91;]HostStatus{HostStatus{\"1\",\"ok\",\"test1\"}, HostStatus{\"2\",\"ok\",\"test2\"}, HostStatus{\"3\",\"ok\",\"test3\"}}\n\tfmt.Printf(\"value = %v\\n\", value)\n\thostnameToStatus := map&#91;string]HostStatus{}\n\thostnameToStatusP := map&#91;string]*HostStatus{}\n\thostnameToStatusP2 := map&#91;string]*HostStatus{}\n\tfor k, hostStatus := range value {\n\t\thostnameToStatus&#91;hostStatus.Name] = hostStatus\n\t\thostnameToStatusP&#91;hostStatus.Name] = &amp;value&#91;k]\n\t\thostnameToStatusP2&#91;hostStatus.Name] = &amp;hostStatus\n\t\tfmt.Printf(\"%s , p= %p\\n\", hostStatus.Name, &amp;hostStatus)\n\t}\n\tfmt.Print(\"--1--\\n\")\n\tfor k, v := range hostnameToStatus {\n\t\tfmt.Printf(\"%s = %s\\n\", k, v)\n\t}\n\tfmt.Print(\"--2--\\n\")\n\tfor k, v := range hostnameToStatusP {\n\t\tfmt.Printf(\"%s = %s\\n\", k, v)\n\t}\n\tfmt.Print(\"--3--\\n\")\n\tfor k, v := range hostnameToStatusP2 {\n\t\tfmt.Printf(\"%s = %v\\n\", k, v)\n\t\tfmt.Printf(\"%s = %p\\n\", k, &amp;v)\n\t}\n\n\tsuccessfulChildHostList := &#91;]string{}\n\tmerged := &#91;]HostStatus{}\n\tfor _, hostStatus := range hostnameToStatusP {\n\t\tmerged = append(merged, *hostStatus)\n\t\tif strings.EqualFold(hostStatus.Status, \"yes\") {\n\t\t\tsuccessfulChildHostList = append(successfulChildHostList, hostStatus.Name)\n\t\t}\n\t}\n\tfmt.Print(\"-----------\\n\")\n\tfmt.Printf(\"merged = %v\\n\", merged)\n\tfmt.Printf(\"successfulChildHostList = %v\\n\", successfulChildHostList)\n\n}<\/code><\/pre>\n\n\n\n<p>Results, see the 3rd one with  duplicated values.:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value = &#91;{1 ok test1} {2 ok test2} {3 ok test3}]\n1 , p= 0xc000068210\n2 , p= 0xc000068210\n3 , p= 0xc000068210\n--1--\n3 = {3 ok test3}\n1 = {1 ok test1}\n2 = {2 ok test2}\n--2--\n1 = &amp;{1 ok test1}\n2 = &amp;{2 ok test2}\n3 = &amp;{3 ok test3}\n--3--\n1 = &amp;{3 ok test3}\n1 = 0xc00000e030\n2 = &amp;{3 ok test3}\n2 = 0xc00000e030\n3 = &amp;{3 ok test3}\n3 = 0xc00000e030\n-----------\nmerged = &#91;{1 ok test1} {2 ok test2} {3 ok test3}]\nsuccessfulChildHostList = &#91;]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s tricky to using pointer in range iterator. Please beware variable &#8220;hostStatus&#8221; is a pointer that&#8217;s been reused in each iteration. the location of hostStatus will be the same although it points to a different struct in each iteration. It may cause duplicated values in the final results, beware! Results, see the 3rd one with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/110"}],"collection":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":1,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":111,"href":"http:\/\/iaastalk.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/111"}],"wp:attachment":[{"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iaastalk.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}