aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/go1.17.go
blob: 2a9fba75c41fcdfc28a11d558f7a8b0e1cc5fd31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

// Test new language features introduced in Go 1.17:
// https://tip.golang.org/doc/go1.17#language
// Once this becomes the minimum Go version of TinyGo, these tests should be
// merged with the regular slice tests.

import "unsafe"

func main() {
	// Test conversion from array to slice.
	slice1 := []int{1, 2, 3, 4}
	arr1 := (*[4]int)(slice1)
	arr1[1] = -2
	arr1[2] = 20
	println("slice to array pointer:", arr1[0], arr1[1], arr1[2], arr1[3])

	// Test unsafe.Add.
	arr2 := [...]int{1, 2, 3, 4}
	*(*int)(unsafe.Add(unsafe.Pointer(&arr2[0]), unsafe.Sizeof(int(1))*1)) = 5
	*addInt(&arr2[0], 2) = 8
	println("unsafe.Add array:", arr2[0], arr2[1], arr2[2], arr2[3])

	// Test unsafe.Slice.
	arr3 := [...]int{1, 2, 3, 4}
	slice3 := unsafe.Slice(&arr3[1], 3)
	slice3[0] = 9
	slice3[1] = 15
	println("unsafe.Slice array:", len(slice3), cap(slice3), slice3[0], slice3[1], slice3[2])
}

func addInt(ptr *int, index uintptr) *int {
	return (*int)(unsafe.Add(unsafe.Pointer(ptr), unsafe.Sizeof(int(1))*index))
}