郭學聰 Hsueh-Tsung Kuo
Sat, 31 Jul 2021
Someone (who?) said:
a game programmer should be able to draw cute anime character(?)
var a [4]int a[0] = 1 i := a[0] // i == 1 // a[2] == 0, the zero value of the int type
b := [2]string{"Penn", "Teller"} // make the compiler count the array elements b := [...]string{"Penn", "Teller"}
func make([]T, len, cap) []T var s []byte s = make([]byte, 5, 5) // s == []byte{0, 0, 0, 0, 0}
s := make([]byte, 5) len(s) == 5 cap(s) == 5
letters := []string{"a", "b", "c", "d"}
type slice struct { array unsafe.Pointer len int cap int }
func makeslice(et *_type, len, cap int) unsafe.Pointer func growslice(et *_type, old slice, cap int) slice func slicecopy(toPtr unsafe.Pointer, toLen int, fromPtr unsafe.Pointer, fromLen int, width uintptr) append() // where?
val := s[2] s[3] = val
// i : begin index (include) // j : end index (exclude) // k : index of capacity boundary (exclude) s := v[i:j:k]
// k <= cap(v) // j <= k s := v[i:j:k]
// Calculate the base pointer (rptr) for the new slice. // // Generate the following code assuming that indexes are in bounds. // The masking is to make sure that we don't generate a slice // that points to the next object in memory. We cannot just set // the pointer to nil because then we would create a nil slice or // string. // // rcap = k - i // rlen = j - i // rptr = ptr + (mask(rcap) & (i * stride)) // // Where mask(x) is 0 if x==0 and -1 if x>0 and stride is the width // of the element type.
a = append(a, b...)
b = make([]T, len(a)) copy(b, a)
b = append([]T(nil), a...)
b = append(a[:0:0], a...)
a = append(a[:i], a[j:]...)
a = append(a[:i], a[i+1:]...)
a = a[:i+copy(a[i:], a[i+1:])]
a[i] = a[len(a)-1] a = a[:len(a)-1]
copy(a[i:], a[j:]) for k, n := len(a)-j+i, len(a); k < n; k++ { a[k] = nil // or the zero value of T } a = a[:len(a)-j+i]
copy(a[i:], a[i+1:]) a[len(a)-1] = nil // or the zero value of T a = a[:len(a)-1]
a[i] = a[len(a)-1] a[len(a)-1] = nil a = a[:len(a)-1]
a = append(a[:i], append(make([]T, j), a[i:]...)...)
a = append(a, make([]T, j)...)
n := 0 for _, x := range a { if keep(x) { a[n] = x n++ } } a = a[:n]
a = append(a[:i], append([]T{x}, a[i:]...)...)
a = append(a[:i], append(b, a[i:]...)...)
a = append(a, x)
x, a = a[len(a)-1], a[:len(a)-1]
a = append([]T{x}, a...)
x, a = a[0], a[1:]
func QuickSort(a []int) { if len(a) <= 1 { return } pivot := rand.Int() % len(a) left, right := 0, len(a)-1 a[pivot], a[right] = a[right], a[pivot] for i, _ := range a[:right] { if a[i] < a[right] { a[left], a[i] = a[i], a[left] left++ } } a[left], a[right] = a[right], a[left] QuickSort(a[:left]) QuickSort(a[left+1:]) }
s := []int{1, 2, 3, 4, 5, 6, 7, 8} fmt.Printf("s == %v\n\n", s) s2 := s[1:4:5] fmt.Printf("s2 == %v\n\n", s2) s2 = append(s2, 15) fmt.Printf("s == %v\n", s) fmt.Printf("s2 == %v\n\n", s2) s3 := s2[:4:4] fmt.Printf("s3 == %v\n\n", s3) s2 = append(s2, 16) fmt.Printf("s == %v\n", s) fmt.Printf("s2 == %v\n\n", s2)
s == [1 2 3 4 5 6 7 8]
s2 == [2 3 4]
s == [1 2 3 4 15 6 7 8]
s2 == [2 3 4 15]
s3 == [2 3 4 15]
s == [1 2 3 4 15 6 7 8]
s2 == [2 3 4 15 16]
成為 slice 大師
郭學聰 Hsueh-Tsung Kuo2021_07_31