{"id":781,"date":"2026-10-06T17:00:00","date_gmt":"2026-10-06T11:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-map-programs\/"},"modified":"2026-11-29T01:29:35","modified_gmt":"2026-11-28T19:59:35","slug":"go-map-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-map-programs\/","title":{"rendered":"Go Map Programs with Output: Count, Group, Merge and Sort"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go map programs store data as key-value pairs, such as a product code and its price or a word and how often it appears. Maps give you fast lookups by key, so they power counters, caches, groups and indexes in almost every Go service. In this post, you will write fourteen map programs with their exact output, and then practice the map questions that trip up many Go developers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We already used small maps in our <a href=\"https:\/\/smitabharat.com\/blog\/go-string-manipulation-programs\/\">Go string manipulation programs<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-searching-programs\/\">Go searching programs<\/a>. For the theory behind maps, read <a href=\"https:\/\/smitabharat.com\/blog\/go-maps-examples\/\">Go maps explained with examples<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Map Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go map programs use the built-in <code>map[K]V<\/code> type, where every key of type K points to one value of type V. Keys are unique, so writing to an existing key replaces its value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a phone contact list. You look up &#8220;Asha&#8221; and get her number straight away, without reading every contact. A map works the same way: give it a key, and it finds the value almost instantly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Map Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Maps solve a huge range of everyday problems. For example, you count page views per URL, cache API results by user ID and group orders by city. Interviewers use Go map programs to test frequency counting, grouping and your knowledge of map rules like random order.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Counting:<\/strong> word counts, vote counts and visitor counts.<\/li>\n\n\n<li><strong>Grouping:<\/strong> students by grade, orders by status, files by type.<\/li>\n\n\n<li><strong>Lookups:<\/strong> config values, caches and ID to name tables.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Map Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A map hashes each key to decide where to store its value. When you read <code>m[key]<\/code>, Go hashes the key again and jumps straight to the right slot. That is why lookups take about the same time whether the map holds ten items or ten million.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-1024x640.png\" alt=\"Go map programs diagram showing keys hashed into buckets that hold values\" class=\"wp-image-754\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Go hashes each key to find its bucket, so reading and writing by key is fast.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the map operations you will use in Go map programs.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operation<\/th><th>Syntax<\/th><th>Notes<\/th><\/tr><\/thead><tbody><tr><td>Create<\/td><td>m := map[string]int{}<\/td><td>Or make(map[string]int)<\/td><\/tr><tr><td>Add or update<\/td><td>m[&#8220;pen&#8221;] = 40<\/td><td>Replaces an old value<\/td><\/tr><tr><td>Read<\/td><td>v := m[&#8220;pen&#8221;]<\/td><td>Missing key gives zero value<\/td><\/tr><tr><td>Check key<\/td><td>v, ok := m[&#8220;pen&#8221;]<\/td><td>ok is false if missing<\/td><\/tr><tr><td>Delete<\/td><td>delete(m, &#8220;pen&#8221;)<\/td><td>Safe if key is missing<\/td><\/tr><tr><td>Length<\/td><td>len(m)<\/td><td>Number of keys<\/td><\/tr><tr><td>Remove all<\/td><td>clear(m)<\/td><td>Since Go 1.21<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Map Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go map programs cover the core actions on a map. If maps are new to you, start with Program 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Your First Map of Fruit Prices<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the smallest useful map. Each fruit name points to its price.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Step 1: create a map. Each fruit name (string) points to a price (int).\n    prices := map[string]int{ \/\/ := creates the variable and sets its value\n        \"apple\":  120,\n        \"banana\": 40,\n    }\n\n    \/\/ Step 2: add a new fruit with its price.\n    prices[\"mango\"] = 150\n\n    \/\/ Step 3: read values by their key.\n    fmt.Println(\"Apple costs\", prices[\"apple\"])\n    fmt.Println(\"Mango costs\", prices[\"mango\"])\n\n    \/\/ len tells you how many keys the map holds.\n    fmt.Println(\"Fruits in the map:\", len(prices))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Apple costs 120\nMango costs 150\nFruits in the map: 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, we create the map with two fruits inside curly braces. Then <code>prices[\"mango\"] = 150<\/code> adds a third fruit. To read a price, you put the key in square brackets, like <code>prices[\"apple\"]<\/code>. Finally, <code>len<\/code> counts the keys, which gives 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Add, Update, Read and Delete<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Step 1: start with two items in stock.\n    stock := map[string]int{\"pen\": 40, \"book\": 12}\n\n    \/\/ Step 2: change the map.\n    stock[\"bag\"] = 5      \/\/ add a new key\n    stock[\"pen\"] = 35     \/\/ the key already exists, so this updates it\n    delete(stock, \"book\") \/\/ remove the key \"book\"\n\n    \/\/ Step 3: read the values. \"book\" is gone, so it reads as 0.\n    fmt.Println(stock[\"pen\"], stock[\"bag\"], stock[\"book\"])\n    fmt.Println(len(stock))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>35 5 0\n2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Writing to a new key adds it, and writing to an old key replaces its value. The built-in <code>delete<\/code> function removes a key. Reading the deleted key &#8220;book&#8221; gives 0, the zero value for int. It does not cause an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Check if a Key Exists<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Coffee is free here, so its price is 0.\n    prices := map[string]float64{\"tea\": 15, \"coffee\": 0}\n    items := []string{\"tea\", \"coffee\", \"juice\"}\n\n    for _, item := range items { \/\/ range gives each index (ignored with _) and item\n        \/\/ ok is true if the key was found in the map, false if not.\n        price, ok := prices[item]\n        if ok {\n            fmt.Printf(\"%s costs %.0f\\n\", item, price) \/\/ %.0f prints a number with no decimals\n        } else {\n            fmt.Printf(\"%s is not on the menu\\n\", item)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>tea costs 15\ncoffee costs 0\njuice is not on the menu<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reading a map can give back two values. The first is the price, and the second, <code>ok<\/code>, says if the key exists. The <code>ok<\/code> flag is the only way to tell &#8220;coffee costs 0&#8221; from &#8220;juice does not exist&#8221;. Use this form whenever zero is a valid value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Print a Map in Sorted Key Order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Map order is random in Go, on purpose. To print in order, sort the keys first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    ages := map[string]int{\"Ravi\": 30, \"Asha\": 25, \"Meera\": 28, \"Kiran\": 35}\n\n    \/\/ Step 1: get all the keys and sort them A to Z.\n    names := slices.Sorted(maps.Keys(ages)) \/\/ maps.Keys gives the keys, slices.Sorted sorts them\n\n    \/\/ Step 2: print each name with its age, in sorted order.\n    for _, name := range names {\n        fmt.Println(name, ages[name])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Asha 25\nKiran 35\nMeera 28\nRavi 30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>maps.Keys<\/code> hands out every key of the map, and <code>slices.Sorted<\/code> collects them into a sorted slice. Then the loop walks that sorted slice and looks up each age. Because the names are sorted, the output is the same on every run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Counting and Grouping Go Map Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go map programs count items and sort them into groups.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Count Votes for Each Color<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Counting is the most common job for a map. Here is the easiest version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    votes := []string{\"red\", \"blue\", \"red\", \"green\", \"red\", \"blue\"}\n\n    \/\/ An empty map: color -&gt; number of votes.\n    count := map[string]int{}\n\n    \/\/ Add one vote for each color we see.\n    for _, color := range votes {\n        count[color]++ \/\/ a missing key starts at 0, so the first ++ makes it 1\n    }\n\n    fmt.Println(\"red:\", count[\"red\"])\n    fmt.Println(\"blue:\", count[\"blue\"])\n    fmt.Println(\"green:\", count[\"green\"])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>red: 3\nblue: 2\ngreen: 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop reads one vote at a time. When a color is new, its count starts at 0, so <code>count[color]++<\/code> makes it 1. When the color comes again, the same line adds one more. You never need to set up each color first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Top Three Words by Count<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n    \"strings\"\n)\n\nfunc main() {\n    text := \"go is fun and go is fast and go is easy to learn\"\n\n    \/\/ Step 1: count how often each word appears.\n    count := map[string]int{}\n    for _, word := range strings.Fields(text) { \/\/ strings.Fields splits the text on spaces\n        count[word]++\n    }\n\n    \/\/ Step 2: put all the words into a slice so we can sort them.\n    words := slices.Collect(maps.Keys(count))\n\n    \/\/ Step 3: sort by count (high to low), then by word (A to Z) for ties.\n    slices.SortFunc(words, func(first, second string) int {\n        if count[first] != count[second] {\n            return count[second] - count[first] \/\/ a negative result puts first before second\n        }\n        return strings.Compare(first, second)\n    })\n\n    \/\/ Step 4: print the first three words. words[:3] means items 0, 1 and 2.\n    for _, word := range words[:3] {\n        fmt.Println(word, count[word])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>go 3\nis 3\nand 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, we count every word, just like the votes in Program 5. Next, we copy the words into a slice and sort it with a small compare function. A higher count comes first, and equal counts fall back to A to Z order. Without that tie-breaker, &#8220;go&#8221; and &#8220;is&#8221; could swap places between runs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Group Students by Grade<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A map of slices groups many values under one key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\n\/\/ gradeFor turns a mark into a letter grade.\nfunc gradeFor(mark int) string {\n    if mark &gt;= 90 {\n        return \"A\"\n    } else if mark &gt;= 75 {\n        return \"B\"\n    }\n    return \"C\"\n}\n\nfunc main() {\n    marks := map[string]int{\"Asha\": 91, \"Ravi\": 76, \"Meera\": 88, \"Kiran\": 64, \"Dev\": 95}\n\n    \/\/ Each grade (key) holds a list of student names (value).\n    groups := map[string][]string{}\n\n    \/\/ Step 1: visit students in A to Z order and put each one in a group.\n    for _, name := range slices.Sorted(maps.Keys(marks)) {\n        grade := gradeFor(marks[name])\n        groups[grade] = append(groups[grade], name) \/\/ append adds the name to the end of the list\n    }\n\n    \/\/ Step 2: print the groups in order A, B, C.\n    for _, grade := range slices.Sorted(maps.Keys(groups)) {\n        fmt.Println(grade, groups[grade])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>A [Asha Dev]\nB [Meera Ravi]\nC [Kiran]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The helper <code>gradeFor<\/code> turns each mark into a letter. Then <code>append<\/code> adds the student&#8217;s name to the list for that letter. <code>append<\/code> works on a missing key, because reading it gives a nil (empty) slice. So we never need to create each group first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Invert a Map<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    codes := map[string]string{\"IN\": \"India\", \"JP\": \"Japan\", \"FR\": \"France\"}\n\n    \/\/ Step 1: build a new map that goes the other way, name -&gt; code.\n    byName := map[string]string{}\n    for code, name := range codes { \/\/ range on a map gives each key and value\n        byName[name] = code\n    }\n\n    \/\/ Step 2: print the new map in sorted order.\n    for _, name := range slices.Sorted(maps.Keys(byName)) {\n        fmt.Println(name, \"-&gt;\", byName[name])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>France -&gt; FR\nIndia -&gt; IN\nJapan -&gt; JP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop takes each code and name pair and stores it the other way round. So the country name becomes the key, and the code becomes the value. This works only when values are unique. If two keys share a value, one of them is lost in the inverted map.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Special Go Map Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go map programs show patterns that you will use in real projects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: A Set with map[string]struct{}<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go has no set type, so we use a map with empty struct values, which take no memory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ The value struct{} is empty. We only care about the keys.\n    visited := map[string]struct{}{}\n    pages := []string{\"\/home\", \"\/about\", \"\/home\", \"\/blog\", \"\/about\"}\n\n    \/\/ Step 1: add every page. A repeated page just overwrites itself.\n    for _, page := range pages {\n        visited[page] = struct{}{} \/\/ struct{}{} is the one empty value\n    }\n\n    \/\/ Step 2: check if \"\/blog\" is in the set. We ignore the value with _.\n    _, seen := visited[\"\/blog\"]\n    fmt.Println(len(visited), seen)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keys in a map are unique, so the repeated pages are stored only once. That leaves 3 pages in the set. To check if a page is there, read it with the two-value form and keep only the second value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Merge Two Maps<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    defaults := map[string]string{\"theme\": \"light\", \"lang\": \"en\", \"size\": \"M\"}\n    user := map[string]string{\"theme\": \"dark\", \"size\": \"L\"}\n\n    \/\/ Step 1: make a full copy, so defaults stays unchanged.\n    settings := maps.Clone(defaults)\n\n    \/\/ Step 2: copy the user's values on top. Matching keys are replaced.\n    maps.Copy(settings, user)\n\n    \/\/ Step 3: print the merged settings in sorted order.\n    for _, key := range slices.Sorted(maps.Keys(settings)) {\n        fmt.Println(key, settings[key])\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>lang en\nsize L\ntheme dark<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>maps.Clone<\/code> gives us a new map with the same keys and values. Then <code>maps.Copy<\/code> writes every user setting into it, so &#8220;theme&#8221; and &#8220;size&#8221; change. The key &#8220;lang&#8221; was not in the user map, so it keeps the default value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Map with Struct Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You cannot change a field of a struct inside a map directly. Read it, change it, and write it back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Account holds an owner's name and their money.\ntype Account struct {\n    Owner   string\n    Balance int\n}\n\nfunc main() {\n    \/\/ Way 1: the map stores full Account values.\n    accounts := map[int]Account{101: {\"Asha\", 5000}}\n    account := accounts[101]  \/\/ Step 1: copy the value out\n    account.Balance += 1500   \/\/ Step 2: change the copy\n    accounts[101] = account   \/\/ Step 3: store it back\n    fmt.Println(accounts[101])\n\n    \/\/ Way 2: the map stores pointers (*Account), which are addresses of accounts.\n    accountPointers := map[int]*Account{102: {\"Ravi\", 2000}}\n    accountPointers[102].Balance -= 500 \/\/ a pointer lets you change the struct in place\n    fmt.Println(*accountPointers[102])  \/\/ * reads the struct the pointer points to\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{Asha 6500}\n{Ravi 1500}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the first way, reading <code>accounts[101]<\/code> gives you a copy. So you change the copy and then store it back in the map. In the second way, the map holds addresses, so you can change the struct directly. Choose the style that fits how often you update.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Nested Map for City Sales<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ addSale adds a quantity for one product in one city.\nfunc addSale(sales map[string]map[string]int, city, product string, quantity int) {\n    \/\/ Step 1: create the inner map for this city if it does not exist yet.\n    if sales[city] == nil { \/\/ nil means \"no map here yet\"\n        sales[city] = map[string]int{}\n    }\n    \/\/ Step 2: add the quantity to the product total.\n    sales[city][product] += quantity\n}\n\nfunc main() {\n    \/\/ Outer key: city. Inner key: product. Value: quantity sold.\n    sales := map[string]map[string]int{}\n    addSale(sales, \"Pune\", \"pen\", 10)\n    addSale(sales, \"Pune\", \"pen\", 5)\n    addSale(sales, \"Delhi\", \"bag\", 3)\n    fmt.Println(sales[\"Pune\"][\"pen\"], sales[\"Delhi\"][\"bag\"], sales[\"Goa\"][\"pen\"])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>15 3 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each city points to its own small map of products. The helper <code>addSale<\/code> first makes sure that inner map exists, then adds the quantity. Reading a missing city like &#8220;Goa&#8221; is safe and gives 0. Writing to a nil inner map, however, would panic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Delete While Iterating<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    carts := map[string]int{\"u1\": 0, \"u2\": 3, \"u3\": 0, \"u4\": 1}\n\n    \/\/ Way 1: delete empty carts inside a normal range loop.\n    for user, items := range carts {\n        if items == 0 {\n            delete(carts, user) \/\/ deleting during range is safe in Go\n        }\n    }\n    fmt.Println(slices.Sorted(maps.Keys(carts)))\n\n    \/\/ Way 2: maps.DeleteFunc removes every key where the function returns true.\n    maps.DeleteFunc(carts, func(user string, items int) bool {\n        return items &lt; 2\n    })\n    fmt.Println(carts)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[u2 u4]\nmap[u2:3]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first loop removes users u1 and u3, because their carts are empty. Go allows deleting keys during a <code>range<\/code> loop. <code>maps.DeleteFunc<\/code> does the same job with a small test function. Here it removes u4, because 1 item is less than 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Compare Two Maps<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n)\n\nfunc main() {\n    first := map[string]int{\"x\": 1, \"y\": 2}\n    second := map[string]int{\"y\": 2, \"x\": 1} \/\/ same data, different writing order\n    third := map[string]int{\"x\": 1}\n\n    \/\/ maps.Equal is true only if both maps have the same keys and values.\n    fmt.Println(maps.Equal(first, second), maps.Equal(first, third))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first two maps hold the same pairs, so they are equal even though we wrote them in a different order. The third map is missing &#8220;y&#8221;, so it is not equal. Maps cannot use <code>==<\/code>, except to compare with nil. <code>maps.Equal<\/code> checks the keys and values for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Map vs Slice in Go Map Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Maps and slices both hold many values, but they suit different jobs. The diagram and table below compare them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-1024x640.png\" alt=\"Go map programs comparison of maps and slices\" class=\"wp-image-755\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Use a map for fast lookups by key, and a slice when order matters or you loop over every item.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Map<\/th><th>Slice<\/th><\/tr><\/thead><tbody><tr><td>Find by<\/td><td>Key<\/td><td>Index<\/td><\/tr><tr><td>Lookup speed<\/td><td>O(1) average<\/td><td>O(n) unless sorted<\/td><\/tr><tr><td>Order<\/td><td>Random<\/td><td>Kept<\/td><\/tr><tr><td>Duplicates<\/td><td>Keys are unique<\/td><td>Allowed<\/td><\/tr><tr><td>Zero value usable<\/td><td>Read yes, write no<\/td><td>Yes, append works<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, pick a map for lookups, counts and groups. Pick a slice for ordered lists and when you visit every item.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Map Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Writing to a nil map:<\/strong> <code>var m map[string]int<\/code> then <code>m[\"a\"] = 1<\/code> panics. Create it with <code>make<\/code> or <code>{}<\/code> first.<\/li>\n\n\n<li><strong>Relying on order:<\/strong> map order changes between runs, so sort keys before printing.<\/li>\n\n\n<li><strong>Missing the ok flag:<\/strong> a zero value does not prove the key exists.<\/li>\n\n\n<li><strong>Changing struct fields in place:<\/strong> <code>m[k].Field = v<\/code> does not compile with struct values.<\/li>\n\n\n<li><strong>Sharing a map across goroutines:<\/strong> plain maps are not safe for concurrent writes. Use a mutex or <code>sync.Map<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Map Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use make with a size hint<\/strong> when you know roughly how many keys you will add.<\/li>\n\n\n<li><strong>Sort keys for output<\/strong>, so logs and tests are stable.<\/li>\n\n\n<li><strong>Use map[K]struct{} for sets<\/strong>, which saves memory.<\/li>\n\n\n<li><strong>Clone before changing<\/strong> maps you received from other code.<\/li>\n\n\n<li><strong>Use the maps package<\/strong> for Keys, Values, Clone, Copy, Equal and DeleteFunc.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Map Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go map programs to test the special rules of maps. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go Map Programs Rule Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What happens when you write to a nil map?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n    var scores map[string]int \/\/ declared but never created, so it is nil\n    scores[\"a\"] = 1           \/\/ writing to a nil map panics\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: assignment to entry in nil map<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A nil map has no storage. Reading from it is fine and returns zero values, but writing panics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var scores map[string]int \/\/ a nil map\n\n    \/\/ Reading a nil map gives the zero value, and len is 0.\n    fmt.Println(scores[\"x\"], len(scores), scores == nil)\n\n    \/\/ A range loop over a nil map simply runs zero times.\n    for key := range scores {\n        fmt.Println(key)\n    }\n\n    \/\/ delete on a nil map does nothing.\n    delete(scores, \"x\")\n    fmt.Println(\"ok\")\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 0 true\nok<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reading, ranging and deleting are all safe on a nil map. Only writing causes a panic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. Why does this program not compile?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\n\/\/ Point is a spot with X and Y positions.\ntype Point struct{ X, Y int }\n\nfunc main() {\n    points := map[string]Point{\"a\": {1, 2}}\n    points[\"a\"].X = 10 \/\/ not allowed: you cannot change a field inside a map\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cannot assign to struct field points[\"a\"].X in map<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Map values are not addressable, because Go may move them when the map grows. Copy the struct out, change it, and store it back, or use pointer values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. Which types can be map keys?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any comparable type: numbers, strings, bools, pointers, channels, arrays and structs made of comparable fields. Slices, maps and functions cannot be keys.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Behaviour Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ addItem writes one new key into the map it receives.\nfunc addItem(items map[string]int) {\n    items[\"new\"] = 1\n}\n\nfunc main() {\n    items := map[string]int{}\n    addItem(items)\n    fmt.Println(items, len(items))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>map[new:1] 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A map value is a small handle to the real data. So a function that writes to the map changes the caller&#8217;s map too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. Is map order random every time?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, by design. Go randomises the starting point of each <code>range<\/code> loop, so code never depends on order by accident. <code>fmt.Println<\/code> of a map is the one exception, because it sorts keys for printing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    letters := map[string]int{\"b\": 2, \"c\": 3, \"a\": 1}\n    fmt.Println(letters) \/\/ fmt sorts map keys before printing\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>map[a:1 b:2 c:3]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A rune is one character. 'l' in single quotes is a rune.\n    counts := map[rune]int{}\n    for _, letter := range \"hello\" { \/\/ range over a string gives each character\n        counts[letter]++\n    }\n    fmt.Println(counts['l'], counts['h'], counts['z'], len(counts))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 1 0 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;hello&#8221; has four different letters, so <code>len<\/code> is 4. The missing &#8216;z&#8217; simply reads as 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    original := map[string]int{\"a\": 1}\n    other := original \/\/ this does NOT copy the map, both names share the same data\n    other[\"a\"] = 100\n    fmt.Println(original[\"a\"])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>100<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assigning a map does not copy it. Both variables point to the same data, so use <code>maps.Clone<\/code> when you need a real copy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"maps\"\n    \"slices\"\n)\n\nfunc main() {\n    letters := map[int]string{3: \"c\", 1: \"a\", 2: \"b\"}\n\n    \/\/ maps.Values gives every value, and slices.Sorted sorts them.\n    values := slices.Sorted(maps.Values(letters))\n    fmt.Println(values)\n\n    \/\/ clear removes every key, but the map itself still exists.\n    clear(letters)\n    fmt.Println(len(letters), letters == nil)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[a b c]\n0 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>clear<\/code> removes every key but keeps the map ready to use. So the map is empty, not nil.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. Are Go maps safe for concurrent use?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. If one goroutine writes while another reads or writes, the program can crash with &#8220;concurrent map writes&#8221;. Protect the map with a <code>sync.Mutex<\/code>, or use <code>sync.Map<\/code> for some special cases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ key joins a city and a year into one map key.\n    type key struct {\n        city string\n        year int\n    }\n    sales := map[key]int{}\n    sales[key{\"Pune\", 2025}] += 400\n    sales[key{\"Pune\", 2026}] += 650\n    sales[key{\"Pune\", 2025}] += 100 \/\/ same key as the first line, so it adds to 400\n    fmt.Println(sales[key{\"Pune\", 2025}], len(sales))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>500 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A struct key lets you look up by two values at once. Here, 2025 and 2026 are separate keys, so there are 2 entries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. How do you get the number of items in a map?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>len(m)<\/code>. It is O(1), because Go keeps the count as items are added and removed. There is no <code>cap<\/code> for maps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task brings several Go map programs together. Write a program that reads a list of orders as &#8220;city,amount&#8221; strings and prints the total sales per city, sorted from the highest total to the lowest. Hint: split each string with <code>strings.Cut<\/code>, convert the amount with <code>strconv.Atoi<\/code> and add it to a <code>map[string]int<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/blog\/maps\">Go maps in action (Go blog)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/maps\">maps package documentation<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Map_types\">Go spec: map types<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go map programs store unique keys that each point to one value.<\/li>\n\n\n<li>Use <code>v, ok := m[k]<\/code> to tell a missing key from a zero value.<\/li>\n\n\n<li>Map order is random, so sort keys with <code>slices.Sorted(maps.Keys(m))<\/code> for output.<\/li>\n\n\n<li>Writing to a nil map panics, but reading, ranging and deleting are safe.<\/li>\n\n\n<li>Maps are shared when assigned or passed, so clone them before changing.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To finish Level 3, we combine loops and 2D slices. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/\">Go matrix programs with output<\/a>, with matrix addition, multiplication and transpose. For more map practice, revisit <a href=\"https:\/\/smitabharat.com\/blog\/go-maps-examples\/\">Go maps explained with examples<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go map programs store data as key-value pairs, such as a product code and its price or a word and how often it appears.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":756,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,30,104],"swapz_series":[],"class_list":["post-781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-maps","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Map Programs: Count, Group, Merge and Sort<\/title>\n<meta name=\"description\" content=\"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-map-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Map Programs: Count, Group, Merge and Sort\" \/>\n<meta property=\"og:description\" content=\"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-map-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-06T11:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Map Programs with Output: Count, Group, Merge and Sort\",\"datePublished\":\"2026-10-06T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/\"},\"wordCount\":2007,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-map-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Maps\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/\",\"name\":\"Go Map Programs: Count, Group, Merge and Sort\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-map-programs-featured.png\",\"datePublished\":\"2026-10-06T11:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:35+00:00\",\"description\":\"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-map-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-map-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Map Programs featured image: word count, groups and merged settings\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-map-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Map Programs with Output: Count, Group, Merge and Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Map Programs: Count, Group, Merge and Sort","description":"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-map-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Map Programs: Count, Group, Merge and Sort","og_description":"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.","og_url":"https:\/\/smitabharat.com\/blog\/go-map-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-06T11:30:00+00:00","article_modified_time":"2026-11-28T19:59:35+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Map Programs with Output: Count, Group, Merge and Sort","datePublished":"2026-10-06T11:30:00+00:00","dateModified":"2026-11-28T19:59:35+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/"},"wordCount":2007,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Maps","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-map-programs\/","name":"Go Map Programs: Count, Group, Merge and Sort","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png","datePublished":"2026-10-06T11:30:00+00:00","dateModified":"2026-11-28T19:59:35+00:00","description":"Go map programs with output: add, check, delete, word count, group, invert, merge, sets and sorted keys, plus map interview questions with answers.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-map-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-map-programs-featured.png","width":2400,"height":1260,"caption":"Go Map Programs featured image: word count, groups and merged settings"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-map-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Map Programs with Output: Count, Group, Merge and Sort"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=781"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/781\/revisions"}],"predecessor-version":[{"id":883,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/781\/revisions\/883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/756"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=781"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}