Skip to content

Commit 9056b0e

Browse files
committed
add go1.20-examples
1 parent 9cd8c5d commit 9056b0e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

go1.20-examples/lang/comparable.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
func doSth[T comparable](t T) {
4+
}
5+
6+
func main() {
7+
n := 2
8+
var i interface{} = n
9+
doSth(i)
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func doSth[T comparable](t1, t2 T) {
4+
if t1 != t2 {
5+
println("unequal")
6+
return
7+
}
8+
println("equal")
9+
}
10+
11+
func main() {
12+
n1 := []byte{2}
13+
n2 := []byte{3}
14+
var i interface{} = n1
15+
var j interface{} = n2
16+
doSth(i, j)
17+
}

go1.20-examples/lang/slice2arr.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func slice2arrOK() {
6+
var sl = []int{1, 2, 3, 4, 5, 6, 7}
7+
var arr = [7]int(sl)
8+
var parr = (*[7]int)(sl)
9+
fmt.Println(sl) // [1 2 3 4 5 6 7]
10+
fmt.Println(arr) // [1 2 3 4 5 6 7]
11+
sl[0] = 11
12+
fmt.Println(arr) // [1 2 3 4 5 6 7]
13+
fmt.Println(parr) // &[11 2 3 4 5 6 7]
14+
}
15+
16+
func slice2arrPanic() {
17+
var sl = []int{1, 2, 3, 4, 5, 6, 7}
18+
fmt.Println(sl)
19+
var arr = [8]int(sl) // panic: runtime error: cannot convert slice with length 7 to array or pointer to array with length 8
20+
fmt.Println(arr) // &[11 2 3 4 5 6 7]
21+
22+
}
23+
24+
func main() {
25+
slice2arrOK()
26+
slice2arrPanic()
27+
}

go1.20-examples/lang/unsafe.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"unsafe"
6+
)
7+
8+
func main() {
9+
var arr = [6]byte{'h', 'e', 'l', 'l', 'o', '!'}
10+
s := unsafe.String(&arr[0], 6)
11+
fmt.Println(s) // hello!
12+
arr[0] = 'j'
13+
fmt.Println(s) // jello!
14+
15+
s1 := "golang"
16+
fmt.Println(s1) // golang
17+
b := unsafe.StringData(s1)
18+
*b = 'h' // fatal error: fault, unexpected fault address 0x10a67e5
19+
fmt.Println(s1)
20+
}

go1.20-examples/library/context.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
myError := fmt.Errorf("%s", "myError")
10+
ctx, cancel := context.WithCancelCause(context.Background())
11+
cancel(myError)
12+
fmt.Println(ctx.Err()) // returns context.Canceled
13+
fmt.Println(context.Cause(ctx)) // returns myError
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
func F[T1 any]() {
4+
type x struct{}
5+
type y = x
6+
}
7+
8+
func main() {
9+
F[int]()
10+
}

0 commit comments

Comments
 (0)