{"id":540,"date":"2022-08-30T21:26:00","date_gmt":"2022-08-30T21:26:00","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=540"},"modified":"2022-09-06T15:41:54","modified_gmt":"2022-09-06T15:41:54","slug":"learning-go-language-day-3","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=540","title":{"rendered":"Learning Go Language &#8211; Day 3"},"content":{"rendered":"\n<p><strong>Maps<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">- Collections of value types that are access via keys\n- Created via literals or via \"make\" function\n- Creation syntax:\n  - name = map[type]type()\n  - Alt creation = make(map[type]type)\n- Members accessed via [key] syntax and .dot syntax\n  - myMap[\"key\"] = \"value\"\n  - myMap.value\n- Check for presence with \"value, ok\" form of result\n- Multiple assignments refer to same underlying data<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example code\nfunc main() {\n  statePopulations := map[string]int {\n\t\t\"California\": 1,\n\t\t\"Texas\": 2,\n\t\t\"Florida\": 3,\n\t\t\"New York\": 4,\n\t\t\"Pennsylvania\": 5,\n\t\t\"Illinois\": 6,\n\t\t\"Ohio\": 7,\n\t}\n\tfmt.Println(statePopulations)\n}\n\/\/ NOTE: The types must be support the ability for equality<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<p><strong>Structs<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">- You can mix-and-match datatypes in structs, unlike in other types like arrays\n- Naming convention is the same: PascalCase for export, camelCase for import\n- Keyed by named field\n- Normally created as types, but anonymous structs are possible\n- Structs are value types, meaning copying them will make a new struct\n- No inheritance, but can use composition via embedding\n- Tags can be added to struct fields to describe fields\n- If there is no data in a certain field, you can ignore that field in the struct entirely<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example code\ntype Doctor struct {\n  number int\n  actorName string\n  companions []string\n}\n\nfunc main() {\n  aDoctor := Doctor {\n  number: 3,\n  actorName: \"Jon Pertwee\",\n  companions: []string {\n    \"Liz Shaw\",\n    \"Jo Grant\",\n    \"Sarah Jane Smith\",\n    },\n  }\n  fmt.Println(aDoctor)\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Maps &#8211; Collections of value types that are access via keys &#8211; Created via literals or via &#8220;make&#8221; function &#8211; Creation syntax: &#8211; name = map[type]type() &#8211; Alt creation = make(map[type]type) &#8211; Members accessed via [key] syntax and .dot syntax &#8211; myMap[&#8220;key&#8221;] = &#8220;value&#8221; &#8211; myMap.value &#8211; Check for presence with &#8220;value, ok&#8221; form of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=540"}],"version-history":[{"count":12,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/540\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}