Another example of slices

The Go code of the slices.go program will hopefully clarify many things about slices for you, and it will be presented in five parts.

The first part of the program contains the expected preamble as well as the definition of two slices:

package main 
 
import ( 
    "fmt" 
) 
 
func main() { 
    aSlice := []int{1, 2, 3, 4, 5} 
    fmt.Println(aSlice) 
    integer := make([]int, 2) 
    fmt.Println(integer) 
    integer = nil 
    fmt.Println(integer)

The second part of the program shows how to use the [:] notation to create a new slice that references an existing array. Remember that you are not creating a copy of the array, just a reference to it, which will be verified in the output of the program:

    anArray := [5]int{-1, -2, -3, -4, -5} 
    refAnArray := anArray[:] 
 
    fmt.Println(anArray) 
    fmt.Println(refAnArray) 
    anArray[4] = -100 
    fmt.Println(refAnArray) 

The third code segment defines a slice with one dimension and another one with two dimensions using the make() function:

    s := make([]byte, 5) 
    fmt.Println(s) 
    twoD := make([][]int, 3) 
    fmt.Println(twoD) 
    fmt.Println() 

As slices are automatically initialized by Go, all of the elements of the two preceding slices will have the zero value of the slice type, which for integers is 0 and for slices is nil. Keep in mind that the elements of a multidimensional slice are slices!

In the fourth part of slices.go that is shown in the next piece of Go code, you will learn how to initialize all the elements of a slice with two dimensions manually:

    for i := 0; i < len(twoD); i++ { 
        for j := 0; j < 2; j++ { 
            twoD[i] = append(twoD[i], i*j) 
        } 
    } 

The preceding Go code shows that in order to expand an existing slice and make it grow, you will need to use the append() function and not reference an index that does not exist! The latter would create a panic: runtime error: index out of range error message! Note that the values of the slice elements have been chosen arbitrarily.

The last part of the program shows you how to use the range keyword to visit and print all of the elements of a slice with two dimensions:

    for _, x := range twoD { 
        for i, y := range x { 
            fmt.Println("i:", i, "value:", y) 
        } 
        fmt.Println() 
    } 
} 

If you execute slices.go, you will get the following output:

$ go run slices.go
[1 2 3 4 5]
[0 0]
[]
[-1 -2 -3 -4 -5]
[-1 -2 -3 -4 -5]
[-1 -2 -3 -4 -100]
[0 0 0 0 0]
[[] [] []]
  
i: 0 value: 0
i: 1 value: 0
   
i: 0 value: 0
i: 1 value: 1
    
i: 0 value: 0
i: 1 value: 2  

It should not come as a surprise to you that the objects of the slice with the two dimensions are initialized to nil and therefore printed as empty. This happens because the zero value for the slice type is nil.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181