{"id":9857,"date":"2022-08-25T05:52:26","date_gmt":"2022-08-25T05:52:26","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9857"},"modified":"2024-04-15T08:30:20","modified_gmt":"2024-04-15T08:30:20","slug":"slice-in-go-language-2","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/","title":{"rendered":"Slice in Go Language"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Slice of Slices in Golang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Slice of Slices in Golang<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go (Golang), a slice of slices refers to a two-dimensional data structure where each element of the outer slice is itself a slice. This concept allows for the creation of dynamic, resizable tables or grids of data.<\/p>\n<p>To visualize this, imagine an outer slice where each element represents a row or a subset of data. Each of these elements (rows) is a slice in itself, containing individual elements that represent columns or data points within that row.<\/p>\n<p>Using slices of slices in Go is flexible and efficient because it allows for jagged arrays, where each row can have a different number of columns. This is unlike traditional two-dimensional arrays, which have a fixed size for each dimension.<\/p>\n<p>Slices of slices are commonly used to model and manipulate tabular data, matrices, or grid-based structures in Go programs. They provide a versatile way to manage data collections dynamically and are particularly useful in scenarios where the size of the data structure may change over time or where irregular data shapes are required. Understanding how to work with slices of slices is essential for effective data management and manipulation in Go programming.<\/p>\n<h4>Slices are similar to <a href=\"https:\/\/go.dev\/\">arrays<\/a>, but are <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">more<\/a> powerful and flexible<strong>.<\/strong><\/h4>\n<ol>\n<li><strong>Go program to illustrate Length and capacity in slice<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var x [6]int = [6]int {1, 2, 3, 4, 5,7}\r\n    var a []int = x[1:3]\r\n    fmt.Println(a)\r\n    fmt.Println(len(a))\r\n    fmt.Println(cap(a))\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Slice_Function&gt; go run slice.go\r\n[2 3]\r\n2\r\n4\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong>Go program to illustrate append function.<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var thing = []string{\"HI\", \"This Is Prwatech  \"}\r\n    fmt.Println(thing)\r\n    things = append(thing, \"!!!\")\r\n    fmt.Println(thing)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var thing = []string{\"HI\", \"This Is Prwatech  \"}\r\n    fmt.Println(thing)\r\n    things = append(thing, \"!!!\")\r\n    fmt.Println(thing)\r\n}\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong>Go program to illustrate Appending with length specified<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    var things = []string{\"HI\", \" This  is a Prwatech Institute of Data Scientist \"}\r\n    fmt.Println(things)\r\n    subject = append(things, \"Analyst \")\r\n    fmt.Println(subject)\r\n    things = append(subject [1 : len(subject)-1])\r\n    fmt.Println(subject)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Slice_Function&gt; go run slice3.go\r\n[HI  Prwatech Institute of Data Scientist ]\r\n[HI  Prwatech Institute of Data Scientist  Analyst]\r\n[ Prwatech Institute of Data Scientist ]\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong>Go program to illustrate slice in string<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n    Names: = [4]string{\r\n        \"Python\", \"Machine Learning\", \"Deep Learning\", \"Java\",\r\n    }\r\n    fmt.Println(names)\r\n    a: = Names[0:2]\r\n    b: = Names[1:3]\r\n    fmt.Println(a, b)\r\n    b[0] = \"***\"\r\n    fmt.Println(a, b)\r\n    fmt.Println(Names)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Slice_Function&gt; go run slice4.go\r\n[Python Machine Learning Deep Learning Java]\r\n[Python Machine Learning] [Machine Learning Deep Learning]\r\n[Python ***] [*** Deep Learning]\r\n[Python *** Deep Learning Java]\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong>Go program to illustrate slice Literals<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    k: = []int {2, 4, 3, 5, 6, 78}\r\n    fmt.Println(k)\r\n    m: = []bool {true, false, true, false, false, true}\r\n    fmt.Println(m)\r\n    t: = []struct {\r\n        i int\r\n        b bool\r\n    }{\r\n        {2, true}, \r\n{4, false},\r\n        {3, true},\r\n        {5, true},\r\n {6, false},\r\n        {78, true},\r\n    }\r\n    fmt.Println(t)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Slice_Function&gt; go run slice5.go\r\n[2 4 3 5 6 78]\r\n[true false true true false true]\r\n[{2 true} {4 false} {3 true} {5 true} {6 false} {78 true}]\r\n<\/pre>\n<\/li>\n<li><strong>Go program to illustrate slicing using addressing<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    s: = []int{2, 3, 4, 5, 6, 7, 8, 9}\r\n    s = s[1:4]\r\n    fmt.Println(s)\r\n    s = s[:2]\r\n    fmt.Println(s)\r\n    s = s[1:]\r\n    fmt.Println(s)\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\Slice_Function&gt; go run slice6.go\r\n[3 4 5]\r\n[3 4]\r\n[4]\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li><strong>Go program to illustrate slicing operation<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    s: = []int{2, 3, 4, 5, 6, 7, 11, 14}\r\n    PrintSlice(s)\r\n    s = s[1:4]\r\n    s = s[:0]\r\n    PrintSlice(s)\r\n    s = s[:4]\r\n    PrintSlice(s)\r\n    s = s[2:]\r\n    PrintSlice(s)\r\n}\r\nfunc PrintSlice(s []int) {\r\n    fmt.Printf(\"len=%d cap=%d %v \\n\", len(s), cap(s), s)\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">C:\\GO_Language\\Slice_Function&gt; go run slice7.go\r\nlen=8 cap=8 [2 3 4 5 6 7 11 14] \r\nlen=0 cap=7 [] \r\nlen=4 cap=7 [3 4 5 6] \r\nlen=2 cap=5 [5 6] \r\nPS C:\\GO_Language\\Slice_Function&gt;\r\n<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Slice of Slices in Golang &nbsp; In Go (Golang), a slice of slices refers to a two-dimensional data structure where each element of the outer slice is itself a slice. This concept allows for the creation of dynamic, resizable tables or grids of data. To visualize this, imagine an outer slice where each element represents [&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":[1575,1577,1576,679],"class_list":["post-9857","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-appending","tag-go-program","tag-length","tag-slicing-in-golang"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Slice of Slices in Golang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Slice of Slices 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=\"Slice of Slices in Golang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Slice of Slices 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\/slice-in-go-language-2\/\" \/>\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-25T05:52:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T08:30:20+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\/slice-in-go-language-2\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/\",\"name\":\"Slice of Slices in Golang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-25T05:52:26+00:00\",\"dateModified\":\"2024-04-15T08:30:20+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Slice of Slices in Golang - Dive deep with our expert instructors and comprehensive curriculum, Enroll Now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Slice in Go Language\"}]},{\"@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":"Slice of Slices in Golang - Prwatech","description":"Master Slice of Slices 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":"Slice of Slices in Golang - Prwatech","og_description":"Master Slice of Slices in Golang - Dive deep with our expert instructors and comprehensive curriculum, Enroll Now.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-25T05:52:26+00:00","article_modified_time":"2024-04-15T08:30:20+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\/slice-in-go-language-2\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/","name":"Slice of Slices in Golang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-25T05:52:26+00:00","dateModified":"2024-04-15T08:30:20+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Slice of Slices in Golang - Dive deep with our expert instructors and comprehensive curriculum, Enroll Now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/slice-in-go-language-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Slice in Go Language"}]},{"@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\/9857","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=9857"}],"version-history":[{"count":3,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9857\/revisions"}],"predecessor-version":[{"id":11514,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9857\/revisions\/11514"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}