16-slices/questions/8-advanced-ops.md
lyric := []string{"show", "me", "my", "silver", "lining"}
part := lyric[1:3:5]
4: General Formula:
[low:high:max]=>length = high - maxandcapacity = max - low.lyric[1:3]is["me" "my"].lyric[1:3:5]is["me" "my" "silver" "lining"]. So,[1:3]is the returned slice, length: 2.[1:3:5]limits the capacity to four because after the 1st element there are only four more elements.
lyric := []string{"show", "me", "my", "silver", "lining"}
part := lyric[:2:2]
part = append(part, "right", "place")
3:
lyric[:2:2]= ["show" "me"]. After the append the part becomes: ["show" "me" "right" "place"] — so it allocates a new backing array.lyricstays the same:["show" "me" "my" "silver" "lining"].
1: Yes! You can use the make function to preallocate a backing array for a slice upfront.
tasks := make([]string, 2)
tasks = append(tasks, "hello", "world")
fmt.Printf("%q\n", tasks)
1:
make([]string, 2)creates a slice with len: 2 and cap: 2, and it sets all the elements to their zero-values.append()appends after the length of the slice (after the first two elements). That's why the first two elements are zero-valued strings but the last two elements are the newly appended elements.
tasks := make([]string, 0, 2)
tasks = append(tasks, "hello", "world")
fmt.Printf("%q\n", tasks)
2:
make([]string, 0, 2)creates a slice with len: 0 and cap: 2.append()appends after the length of the slice (at the beginning). That's why the first two elements are overwritten with the newly appended elements. This is a common usage pattern when you want to use themakeand theappendfunctions together.
lyric := []string{"le", "vent", "nous", "portera"}
n := copy(lyric, make([]string, 4))
fmt.Printf("%d %q\n", n, lyric)
// -- USEFUL INFORMATION (but not required to solve the question) --
// In the following `copy` operation, `make` won't allocate
// a slice with a new backing array up to 32768 bytes
// (one string value is 8 bytes on a 64-bit machine).
//
// This is an optimization made by the Go compiler.
3:
copycopies a newly created slice with four elements (make([]string, 4)) ontolyricslice. They both have 4 elements, so thecopycopies 4 elements. Remember:make()initializes a slice with zero-values of its element type. Here, this operation clears all the slice elements to their zero-values.
spendings := [][]int{{200, 100}, {}, {50, 25, 75}, {500}}
total := spendings[2][1] + spendings[3][0] + spendings[0][0]
fmt.Printf("%d\n", total)
1:
spendings[2][1]= 25.spendings[3][0]= 500.spendings[0][0]= 200. 25 + 500 + 200 = 725
spendings := [][]int{{1,2}}
// REMEMBER: %T prints the type of a given value
fmt.Printf("%T - ", spendings)
fmt.Printf("%T - ", spendings[0])
fmt.Printf("%T", spendings[0][0])
2:
spendingsis a 2-dimensional int slice, so its type is [][]int. Its element type is:[]int, so:spendings[0]is[]int.spendings[0]'s element type is:int. Sospendings[0][0]'s type isint.
[][][3]int{{{10, 5, 9}}}
3:
[][][3]intis a multi-dimensional slice of[][3]intelements.[][3]intis a multi-dimensional slice of[3]intelements.[3]intis an array of 3intvalues.