Slice in Go Language

  • date 25th August, 2022 |
  • by Prwatech |
  • 0 Comments

Slice of Slices in Golang

  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 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. 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. 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.

Slices are similar to arrays, but are more powerful and flexible.

  1. Go program to illustrate Length and capacity in slice
    package main
    
    import "fmt"
    
    func main() {
        var x [6]int = [6]int {1, 2, 3, 4, 5,7}
        var a []int = x[1:3]
        fmt.Println(a)
        fmt.Println(len(a))
        fmt.Println(cap(a))
    }
    
    Output :
    PS C:\GO_Language\Slice_Function> go run slice.go
    [2 3]
    2
    4
    
     
  2. Go program to illustrate append function.
    package main
    import "fmt"
    
    func main() {
        var thing = []string{"HI", "This Is Prwatech  "}
        fmt.Println(thing)
        things = append(thing, "!!!")
        fmt.Println(thing)
    }
    
    Output :
    package main
    import "fmt"
    
    func main() {
        var thing = []string{"HI", "This Is Prwatech  "}
        fmt.Println(thing)
        things = append(thing, "!!!")
        fmt.Println(thing)
    }
    
     
  3. Go program to illustrate Appending with length specified
    package main
    
    import "fmt"
    
    func main() {
        var things = []string{"HI", " This  is a Prwatech Institute of Data Scientist "}
        fmt.Println(things)
        subject = append(things, "Analyst ")
        fmt.Println(subject)
        things = append(subject [1 : len(subject)-1])
        fmt.Println(subject)
    }
    
    Output :
    PS C:\GO_Language\Slice_Function> go run slice3.go
    [HI  Prwatech Institute of Data Scientist ]
    [HI  Prwatech Institute of Data Scientist  Analyst]
    [ Prwatech Institute of Data Scientist ]
    
     
  4. Go program to illustrate slice in string
    package main
    import "fmt"
    
    func main() {
        Names: = [4]string{
            "Python", "Machine Learning", "Deep Learning", "Java",
        }
        fmt.Println(names)
        a: = Names[0:2]
        b: = Names[1:3]
        fmt.Println(a, b)
        b[0] = "***"
        fmt.Println(a, b)
        fmt.Println(Names)
    }
    
    Output :
    PS C:\GO_Language\Slice_Function> go run slice4.go
    [Python Machine Learning Deep Learning Java]
    [Python Machine Learning] [Machine Learning Deep Learning]
    [Python ***] [*** Deep Learning]
    [Python *** Deep Learning Java]
    
     
  5. Go program to illustrate slice Literals
    package main
    import "fmt"
    func main() {
        k: = []int {2, 4, 3, 5, 6, 78}
        fmt.Println(k)
        m: = []bool {true, false, true, false, false, true}
        fmt.Println(m)
        t: = []struct {
            i int
            b bool
        }{
            {2, true}, 
    {4, false},
            {3, true},
            {5, true},
     {6, false},
            {78, true},
        }
        fmt.Println(t)
    }
    
    Output :
    PS C:\GO_Language\Slice_Function> go run slice5.go
    [2 4 3 5 6 78]
    [true false true true false true]
    [{2 true} {4 false} {3 true} {5 true} {6 false} {78 true}]
    
  6. Go program to illustrate slicing using addressing
    package main
    import "fmt"
    func main() {
        s: = []int{2, 3, 4, 5, 6, 7, 8, 9}
        s = s[1:4]
        fmt.Println(s)
        s = s[:2]
        fmt.Println(s)
        s = s[1:]
        fmt.Println(s)
    
    Output :
    PS C:\GO_Language\Slice_Function> go run slice6.go
    [3 4 5]
    [3 4]
    [4]
    
     
  7. Go program to illustrate slicing operation
    package main
    import "fmt"
    func main() {
        s: = []int{2, 3, 4, 5, 6, 7, 11, 14}
        PrintSlice(s)
        s = s[1:4]
        s = s[:0]
        PrintSlice(s)
        s = s[:4]
        PrintSlice(s)
        s = s[2:]
        PrintSlice(s)
    }
    func PrintSlice(s []int) {
        fmt.Printf("len=%d cap=%d %v \n", len(s), cap(s), s)
    }
    
    Output :
    C:\GO_Language\Slice_Function> go run slice7.go
    len=8 cap=8 [2 3 4 5 6 7 11 14] 
    len=0 cap=7 [] 
    len=4 cap=7 [3 4 5 6] 
    len=2 cap=5 [5 6] 
    PS C:\GO_Language\Slice_Function>
    
     

Quick Support

image image