I am currently reading *** Go Programming Language, by Alan Donovan***. In chapter 4, I stuck in some code of exercise 4.4 which state that “Write a version of rotate that operates in a single pass?” I want to write it with a little modification with number of left rotations.
package main
import(
"fmt"
)
func rotate(s []int,shift int){
first := s[:shift]
copy(s,s[shift:]) // This change the values of first
s[len(s)-shift]=first // This doesn't work !!!
}
func main() {
data := []int{1,2,3,4,5,6}
left_shift := 3
rotate(data,left_shift) // 4 5 6 1 2 3
fmt.Println(data)
}
Can anyone help me to understand concepts of Slices.