github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/gc/testdata/slice.go (about)

     1  // run
     2  
     3  // This test makes sure that t.s = t.s[0:x] doesn't write
     4  // either the slice pointer or the capacity.
     5  // See issue #14855.
     6  
     7  package main
     8  
     9  import "fmt"
    10  
    11  const N = 1000000
    12  
    13  type T struct {
    14  	s []int
    15  }
    16  
    17  func main() {
    18  	done := make(chan struct{})
    19  	a := make([]int, N+10)
    20  
    21  	t := &T{a}
    22  
    23  	go func() {
    24  		for i := 0; i < N; i++ {
    25  			t.s = t.s[1:9]
    26  		}
    27  		done <- struct{}{}
    28  	}()
    29  	go func() {
    30  		for i := 0; i < N; i++ {
    31  			t.s = t.s[0:8] // should only write len
    32  		}
    33  		done <- struct{}{}
    34  	}()
    35  	<-done
    36  	<-done
    37  
    38  	ok := true
    39  	if cap(t.s) != cap(a)-N {
    40  		fmt.Printf("wanted cap=%d, got %d\n", cap(a)-N, cap(t.s))
    41  		ok = false
    42  	}
    43  	if &t.s[0] != &a[N] {
    44  		fmt.Printf("wanted ptr=%p, got %p\n", &a[N], &t.s[0])
    45  		ok = false
    46  	}
    47  	if !ok {
    48  		panic("bad")
    49  	}
    50  }