github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section3/slicedemo/slicedemo.go (about) 1 // An example showing how to declare a slice and perform various operations using a slice. 2 package main 3 4 import "fmt" 5 6 // Slices get passed by reference into functions, meaning, if make changes to 7 // a slice within a function, our changes will be reflected to the slice that 8 // was passed into the function. 9 10 func populateIntegerSlice(input []int) { 11 12 // We set values in a slice, just like we do with arrays using the square brackets 13 // notation [] with the element index enclosed within the square brackets. 14 input[0] = 3 15 input[1] = 6 16 input[2] = 9 17 input[3] = 12 18 input[4] = 15 19 } 20 21 func changingLineupExample() { 22 23 // Declaring and initializing a slice representing the original band members of 24 // rock band, INXS using a slice literal (notice it looks just like an array literal 25 // except without the element count) 26 fmt.Println("The original INXS lineup:") 27 inxs := []string{"Michael", "Andrew", "Jon", "Tim", "Kirk", "Garry"} 28 fmt.Println(inxs, "\n") 29 30 // Michael left the band in 1997 31 fmt.Println("The lineup without Michael:") 32 inxs = append(inxs[:0], inxs[1:]...) 33 fmt.Println(inxs, "\n") 34 35 // JD joins the band in 2005 36 fmt.Println("The lineup with a new member, JD:") 37 inxs = append(inxs, "JD") 38 fmt.Println(inxs, "\n") 39 40 } 41 42 func main() { 43 44 // We use the make() built-in function to create a new slice of length 5. 45 // Here we make a slice of integers of length 5. 46 var mySlice []int = make([]int, 5) 47 fmt.Printf("Contents of mySlice: %v\n\n", mySlice) 48 49 populateIntegerSlice(mySlice) 50 fmt.Printf("Contents of mySlice: %v\n", mySlice) 51 // We can use the len() built-in function to return the length of the slice 52 fmt.Println("The length of mySlice is: ", len(mySlice)) 53 // We can use the cap() built-in function to return the capacity of the slice 54 fmt.Println("The capacity of mySlice is: ", cap(mySlice), "\n") 55 56 // Add a new element to mySlice, and notice the changes to the length and 57 // capacity of the slice 58 fmt.Println("After adding a new element to mySlice...\n") 59 mySlice = append(mySlice, 18) 60 fmt.Printf("Contents of mySlice: %v\n", mySlice) 61 fmt.Println("The length of mySlice is: ", len(mySlice)) 62 fmt.Println("The capacity of mySlice is: ", cap(mySlice), "\n") 63 64 // This short hand notation allows us to get the elements from index 1 up to 65 // (but not including) index 4. 66 s := mySlice[1:4] 67 fmt.Println("mySlice[1:4] ", s, "\n") 68 69 // When you slice a slice, you get a reference back to that slice. Any changes you 70 // make to the subslice will be reflected in the original slice. 71 s[0] = 7 // this will cause mySlice[1] to be equal to 7 72 fmt.Println("mySlice: ", mySlice) 73 74 // All elements in myslice up to (not including) the element at index 4 75 t := mySlice[:4] 76 fmt.Println("mySlice[:4] ", t, "\n") 77 78 // The elements from (and including) the element at index 1 79 u := mySlice[1:] 80 fmt.Println("mySlice[1:] ", u, "\n") 81 82 changingLineupExample() 83 }