{"id":9863,"date":"2022-08-27T07:14:33","date_gmt":"2022-08-27T07:14:33","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9863"},"modified":"2024-04-15T10:10:57","modified_gmt":"2024-04-15T10:10:57","slug":"range-and-mapping-in-golang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/","title":{"rendered":"Range and mapping in Golang"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Range Keyword in Golang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Range Keyword in Golang<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go (Golang), the <code>range<\/code> keyword is used to iterate over elements of various data structures, including arrays, slices, strings, maps, and channels. It enables efficient and idiomatic iteration without needing explicit index manipulation or iteration variables.<\/p>\n<p>When used with arrays, slices, or strings, <code>range<\/code> iterates over each element, returning the index and value of each iteration. This simplifies the process of iterating through collections and accessing individual elements.<\/p>\n<p>For maps, <code>range<\/code> iterates over key-value pairs, providing both the key and value for each iteration. This allows convenient access to map entries without manually extracting keys or values.<\/p>\n<p>When used with channels, <code>range<\/code> can be used to iterate over values received from the channel until the channel is closed, facilitating safe and efficient communication between goroutines.<\/p>\n<p>Overall, the <code>range<\/code> keyword in Go promotes clean, concise, and readable code by abstracting away low-level details of iteration. It enhances the expressiveness and simplicity of Go programs by providing a unified and consistent approach to iterating over various data structures and communication channels. Understanding and effectively using <code>range<\/code> is essential for writing efficient and idiomatic Go code.<\/p>\n<p>The\u00a0<strong>range<\/strong> keyword is mainly use for loops to iterate <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">over<\/a> all the <a href=\"https:\/\/go.dev\/\">elements<\/a> of a\u00a0map, slice, channel, or array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nvar Prwa = []int{1, 3, 9, 27, 81, 243, 729, 2187}\r\n\r\nfunc main() {\r\n    for i, x := range Prwa {\r\n        fmt.Printf(\"3**%d=%d\\n\", i, x)\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\range&gt; go run range.go\r\n3**0=1\r\n3**1=3\r\n3**2=9\r\n3**3=27\r\n3**4=81\r\n3**5=243\r\n3**6=729\r\n3**7=2187\r\n<\/pre>\n<ol>\n<li><strong>Initializing slices using Range.<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    pow := make([]int, 10)\r\n    for i:= range pow {\r\n        pow[i] = i &lt;&lt; uint(i)\r\n    }\r\n\r\n    for _, value:= range pow {\r\n        fmt.Printf(\"%d\\n\", value)\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\range&gt; go run range2.go\r\n0\r\n2\r\n8\r\n24\r\n64\r\n160\r\n384\r\n896\r\n2048\r\n4608<\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>Simple Map program <\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var map_1 map[int]int\r\n    if map_1 == nil {\r\n        fmt.Println(\"True\")\r\n    } else {\r\n        fmt.Println(\"False\")\r\n    }\r\n\r\n    map_2 := map[int]string{\r\n        90: \"Red\",\r\n        91: \"Yellow\",\r\n        92: \"Orange\",\r\n        93: \"Green\",\r\n        94: \"Pink\",\r\n        95: \"Blue\",\r\n        96: \" \",\r\n    }\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=\"raw\">PS C:\\GO_Language\\range&gt; go run range3.go\r\nTrue\r\nMap_2 map[90:Red 91:Yellow 92:Orange 93:Green 94:Pink 95:Blue 96: ]\r\n<\/pre>\n<p>Range Keyword in Golang<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Range Keyword in Golang &nbsp; In Go (Golang), the range keyword is used to iterate over elements of various data structures, including arrays, slices, strings, maps, and channels. It enables efficient and idiomatic iteration without needing explicit index manipulation or iteration variables. When used with arrays, slices, or strings, range iterates over each element, returning [&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":[1584,1582,1581,1583],"class_list":["post-9863","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-map-program","tag-mapping-in-go-lang","tag-range-in-go-lang","tag-range-keyword"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Range Keyword in Golang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.\" \/>\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=\"Range Keyword in Golang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-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-27T07:14:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T10:10:57+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/\",\"name\":\"Range Keyword in Golang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-27T07:14:33+00:00\",\"dateModified\":\"2024-04-15T10:10:57+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Range and mapping 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":"Range Keyword in Golang - Prwatech","description":"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.","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":"Range Keyword in Golang - Prwatech","og_description":"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-27T07:14:33+00:00","article_modified_time":"2024-04-15T10:10:57+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/","name":"Range Keyword in Golang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-27T07:14:33+00:00","dateModified":"2024-04-15T10:10:57+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Range Keyword in Golang - Dive deep with our expert instructors and comprehensive curriculum. Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/range-and-mapping-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Range and mapping 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\/9863","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=9863"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9863\/revisions"}],"predecessor-version":[{"id":11532,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9863\/revisions\/11532"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}