Source file test/strcopy.go

     1  // run
     2  
     3  // Copyright 2018 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test that string([]byte(string)) makes a copy and doesn't reduce to
     8  // nothing. (Issue 25834)
     9  
    10  package main
    11  
    12  import (
    13  	"reflect"
    14  	"unsafe"
    15  )
    16  
    17  func main() {
    18  	var (
    19  		buf      = make([]byte, 2<<10)
    20  		large    = string(buf)
    21  		sub      = large[10:12]
    22  		subcopy  = string([]byte(sub))
    23  		subh     = *(*reflect.StringHeader)(unsafe.Pointer(&sub))
    24  		subcopyh = *(*reflect.StringHeader)(unsafe.Pointer(&subcopy))
    25  	)
    26  	if subh.Data == subcopyh.Data {
    27  		panic("sub and subcopy have the same underlying array")
    28  	}
    29  }
    30  

View as plain text