{"id":9859,"date":"2022-08-26T09:21:50","date_gmt":"2022-08-26T09:21:50","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9859"},"modified":"2024-04-15T10:07:02","modified_gmt":"2024-04-15T10:07:02","slug":"maps-in-golang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/","title":{"rendered":"Maps in Golang"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;How to make and use maps in Golang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">How to make and use maps in Golang<\/span><\/h2>\n<p>Maps in Go (or Golang) are a fundamental data structure used for storing key-value pairs. A map allows efficient lookup, insertion, deletion, and update operations based on keys. In Go, a map is declared using the <code>map<\/code> keyword followed by the key and value types enclosed in square brackets (<code>map[keyType]valueType<\/code>).<\/p>\n<p>Maps in Go are unorder collections, meaning that the iteration order of elements is not guarante. The keys within a map must be unique, and each key can map to exactly one value.<\/p>\n<p>To interact with maps in Go, you can use built-in functions like <code>make<\/code> to create a new map instance, <code>len<\/code> to get the number of key-value pairs, and the <code>delete<\/code> function to remove a key-value pair from the map. Accessing a value associated with a key can be done using square brackets (<code>map[key]<\/code> syntax), which returns the value if the key exists or a zero-value if the key is not present.<\/p>\n<p>Go maps are widely use in various applications for efficient data retrieval and management, such as caching, data indexing, and maintaining associative relationships between entities. Understanding how to create, manipulate, and utilize maps is essential for effective Go programming and building scalable applications.<\/p>\n<h4><strong>Using Simple example we <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">will<\/a> <a href=\"https:\/\/go.dev\/\">learn<\/a> mapping in Go lang :<\/strong><\/h4>\n<p>&nbsp;<\/p>\n<ol>\n<li>Go program to illustrate how to create maps\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\nvar map_1 map[int]int   \/\/ Creating and initializing empty map\r\n\r\nif map_1 == nil      \/\/ Checking if the map is nil or not\r\n\r\n        fmt.Println(\"True\")\r\n    } else {\r\n        fmt.Println(\"False\")\r\n    }\r\n\r\n  \t\/\/ Creating and initializing a map\r\n    map_2: = map[int]string {\r\n        90: \"Math\",\r\n        91: \"Statistic\",\r\n        92: \"Python\",\r\n        93: \"SQL\",\r\n        94: \"Machine Learning \",\r\n    }\r\n    fmt.Println(\"Map-2: \", map_2)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Maps&gt; go run maps.go\r\nTrue\r\nMap-2:  map[90:Math 91:Statistic 92:Python 93:SQL 94:Tablue]\r\n<\/pre>\n<\/li>\n<li>Go program to illustrate, iterate a map using the range for loop. The value of this loop may vary because the map is an unordered collection\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    M_a_p: = map[int]string {\r\n        90: \"Math\",\r\n        91: \"Statistic\",\r\n        92: \"Python\",\r\n        93: \"SQL\",\r\n        94: \" Machine Learning \",\r\n    }\r\n    for id, pet := range m_a_p {\r\n        fmt.Println(id, pet)\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Maps&gt; go run maps1.go\r\n92 Python\r\n93 SQL\r\n94  Machine Learning \r\n90 Math\r\n91 Statistic\r\n<\/pre>\n<h3><strong>\u00a0<\/strong><strong>\u00a0 \u00a0<\/strong><\/h3>\n<h3><\/h3>\n<h3><strong>\u00a0 \u00a0Using the Mark Function:<\/strong><\/h3>\n<ol>\n<li>Go program to illustrate the difference between declaring an empty map using with the make()function and without it\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var My_course = make(map[float64]string)\r\n    fmt.Println(My_map)\r\n\r\n    My_course[1.3] = \"Data Scientist\"\r\n    My_course[1.5] = \"Data Analyst\"\r\n    fmt.Println(My_course)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Maps&gt; go run maps2.go\r\nmap[]\r\nmap[1.3:Data Scientist 1.5:Data Analyst]\r\n<\/pre>\n<\/li>\n<li>Go program to illustrate the difference between declaring an empty map using with the make()function and without it.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\n\/\/ Main function\r\nfunc main() {\r\n\r\n    \/\/ Creating and initializing a map\r\n    m_a_p := map[int]string{\r\n\r\n        90: \"Math\",\r\n        91: \"Statistic\",\r\n        92: \"Python\",\r\n        93: \"SQL\",\r\n        94: \"Tablue\",\r\n    }\r\n\r\n    \/\/ Iterating map using for rang loop\r\n    for id, pet := range m_a_p {\r\n\r\n        fmt.Println(id, pet)\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Maps&gt; go run maps3.go\r\n90 Math\r\n91 Statistic\r\n92 Python\r\n93 SQL\r\n94 Tablue\r\n<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>How to make and use maps in Golang Maps in Go (or Golang) are a fundamental data structure used for storing key-value pairs. A map allows efficient lookup, insertion, deletion, and update operations based on keys. In Go, a map is declared using the map keyword followed by the key and value types enclosed in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[666,1707],"tags":[2030,2031,1578,1580,1579],"class_list":["post-9859","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-creating-maps-in-golang-step-by-step-tutorial","tag-go-maps-in-action","tag-map-in-go-language","tag-mark-function","tag-simple"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Make and Use Maps in Golang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Make and Use Maps in Golang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-26T09:21:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T10:07:02+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/\",\"name\":\"How to Make and Use Maps in Golang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-26T09:21:50+00:00\",\"dateModified\":\"2024-04-15T10:07:02+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Maps in Golang\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Make and Use Maps in Golang - Prwatech","description":"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"How to Make and Use Maps in Golang - Prwatech","og_description":"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-26T09:21:50+00:00","article_modified_time":"2024-04-15T10:07:02+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/","name":"How to Make and Use Maps in Golang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-26T09:21:50+00:00","dateModified":"2024-04-15T10:07:02+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master How to Make and Use Maps in Golang - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/maps-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Maps in Golang"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=9859"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9859\/revisions"}],"predecessor-version":[{"id":11530,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9859\/revisions\/11530"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}