{"id":9842,"date":"2022-08-24T06:44:54","date_gmt":"2022-08-24T06:44:54","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9842"},"modified":"2024-04-15T09:59:54","modified_gmt":"2024-04-15T09:59:54","slug":"array-in-golang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/","title":{"rendered":"Array In Golang"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Understanding Arrays and Slices in Go&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Understanding Arrays and Slices in Go<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>Arrays and slices are fundamental data structures in the Go programming language for managing collections of elements.<\/p>\n<p>An array in Go is a fixed-size sequence of elements of the same type. The size of an array is specified at compile time and cannot be changed at runtime. Arrays provide direct access to individual elements using zero-based indexing.<\/p>\n<p>Slices, on the other hand, are dynamic and flexible wrappers around arrays. A slice is a lightweight data structure that represents a segment of an underlying array. Unlike arrays, slices can grow or shrink dynamically at runtime. Slices include a pointer to the underlying array, along with a length and capacity, allowing for efficient operations like appending elements.<\/p>\n<p>Understanding the differences between arrays and slices is crucial for effective memory management and data manipulation in Go. Arrays are suitable for fixed-size collections, while slices are preferred for handling variable-size data and supporting dynamic operations. Both arrays and slices are integral to Go&#8217;s idiomatic approach to managing collections and data structures efficiently.<\/p>\n<p>In an array, store zero or more <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">than<\/a> zero <a href=\"https:\/\/go.dev\/\">elements<\/a> in it. which means the index of the first element is\u00a0<em>array [0]<\/em>\u00a0and the index of the last element is\u00a0an <em>array <strong>[len(array)-1]<\/strong><\/em><strong><em>.<\/em><\/strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-9843\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg\" alt=\"\" width=\"515\" height=\"127\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg 300w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image.jpg 642w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/p>\n<h3>Arrays are created in two different ways:<\/h3>\n<ol>\n<li><strong>Using <\/strong><strong>var keyword<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong>Go program to illustrate how to create<\/strong>:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var a [5]int\r\n    fmt.Println(\" size of the array: \", a)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr.go\r\nthr size of the array [0 0 0 0 0]\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate the values of an array<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    a: = [5]int {1, 3, 4, 2, 5}\r\n    sum: = 0\r\n    for i: = 0; i &lt; len(a); i++ {\r\n        sum += a[i]\r\n        fmt.Println(\"The value :\", sum)\r\n    }\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr2.go\r\nThe value : 1\r\nThe value : 4\r\nThe value : 8\r\nThe value : 10\r\nThe value : 15\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate and insert the values in an array using for loop<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var n [10]int\r\n    var i, j int\r\n    for i = 0; i &lt; 10; i++ {\r\n        n[i] = i + 100\r\n    }\r\n    for j = 0; j &lt; 10; j++ {\r\n        fmt.Printf(\"Element[%d] =%d\\n\", j, n[j])\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr3.go\r\nElement[0] =100\r\nElement[1] =101\r\nElement[2] =102\r\nElement[3] =103\r\nElement[4] =104\r\nElement[5] =105\r\nElement[6] =106\r\nElement[7] =107\r\nElement[8] =108\r\nElement[9] =109\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate the Address of a variable<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    x: = 7\r\n    y: = &amp;x\r\n    fmt.Println(x, y)\r\n\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr4.go\r\n7 0xc000014088\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate Assigning values through pointers<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\nfunc main() {\r\n    i, j: = 42, 2372\r\n    p: = &amp;i\r\n    fmt.Println(*p)\r\n    *p = 21\r\n    fmt.Println(i)\r\n    p = &amp;j\r\n    *p = *p \/ 37\r\n    fmt.Println(j)\r\n\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr5.go\r\n42\r\n21\r\n64\r\nPS C:\\GO_Language\\Array&gt;\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate the value of null pointer<\/strong>.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var ptr *int\r\n    fmt.Printf(\"the Value of ptr is :%x\\n\", ptr)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Array&gt; go run arr6.go\r\nthe Value of ptr is :0\r\n \r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate the division using pointer<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    i, j: = 42, 2372\r\n    p: = &amp;i\r\n    fmt.Println(*p)\r\n    fmt.Printf(\"%T \\n \", p)\r\n\r\n    *p = 21\r\n    fmt.Println(i)\r\n    p = &amp;j\r\n    *p = *p \/ 37\r\n    fmt.Println(j)\r\n\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS C:\\GO_Language\\Array&gt; go run arr7.go\r\n42\r\n*int \r\n 21\r\n64\r\n<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Understanding Arrays and Slices in Go &nbsp; Arrays and slices are fundamental data structures in the Go programming language for managing collections of elements. An array in Go is a fixed-size sequence of elements of the same type. The size of an array is specified at compile time and cannot be changed at runtime. Arrays [&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":[2028,1568,1571,2029,1570,1569],"class_list":["post-9842","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-arrays","tag-go-array","tag-illustrate","tag-slices-and-maps","tag-using-for-loop","tag-using-keywords"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding Arrays and Slices in Go - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Understanding Arrays and Slices in Go - 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=\"Understanding Arrays and Slices in Go - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Understanding Arrays and Slices in Go - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/array-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-24T06:44:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T09:59:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg\" \/>\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\/array-in-golang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/\",\"name\":\"Understanding Arrays and Slices in Go - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg\",\"datePublished\":\"2022-08-24T06:44:54+00:00\",\"dateModified\":\"2024-04-15T09:59:54+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Understanding Arrays and Slices in Go - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image.jpg\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image.jpg\",\"width\":642,\"height\":159},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Array 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":"Understanding Arrays and Slices in Go - Prwatech","description":"Master Understanding Arrays and Slices in Go - 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":"Understanding Arrays and Slices in Go - Prwatech","og_description":"Master Understanding Arrays and Slices in Go - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-24T06:44:54+00:00","article_modified_time":"2024-04-15T09:59:54+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg","type":"","width":"","height":""}],"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\/array-in-golang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/","name":"Understanding Arrays and Slices in Go - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image-300x74.jpg","datePublished":"2022-08-24T06:44:54+00:00","dateModified":"2024-04-15T09:59:54+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Understanding Arrays and Slices in Go - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image.jpg","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2022\/08\/image.jpg","width":642,"height":159},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/array-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Array 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\/9842","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=9842"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9842\/revisions"}],"predecessor-version":[{"id":11528,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9842\/revisions\/11528"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}