This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
append1.go2
113 lines (89 loc) · 3.1 KB
/
append1.go2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// append1 illustrates the properties of the "append" variation described in the
// Type Parameters draft design.
package main
import (
"context"
"fmt"
)
func append(type T)(s []T, t ...T) []T {
lens := len(s)
tot := lens + len(t)
if tot < 0 {
panic("Append: cap out of range")
}
if tot > cap(s) {
news := make([]T, tot, tot+tot/2)
copy(news, s)
s = news
}
s = s[:tot]
copy(s[lens:], t)
return s
}
type Funcs []func()
type Cancels []context.CancelFunc
type Recv <-chan int
var (
f func()
cancel context.CancelFunc
funcSlice []func()
cancelSlice []context.CancelFunc
funcs Funcs
cancels Cancels
r <-chan int
recvSlice []<-chan int
R Recv
RecvSlice []Recv
b chan int
bidiSlice []chan int
)
func main() {
ff := append(funcSlice, f)
fmt.Printf("append(%T, %T) = %T\n", funcSlice, f, ff)
// returns type []func() instead of Funcs
Ff := append(funcs, f)
fmt.Printf("append(%T, %T) = %T\n", funcs, f, Ff)
fc := append(funcSlice, cancel)
fmt.Printf("append(%T, %T) = %T\n", funcSlice, cancel, fc)
cf := append(cancelSlice, f)
fmt.Printf("append(%T, %T) = %T\n", cancelSlice, f, cf)
// returns type []func instead of Funcs
Fc := append(funcs, cancel)
fmt.Printf("append(%T, %T) = %T\n", funcs, cancel, Fc)
Cc := append(cancels, f)
fmt.Printf("append(%T, %T) = %T\n", cancels, f, Cc)
ffc := append(funcSlice, f, cancel)
fmt.Printf("append(%T, %T, %T) = %T\n", funcSlice, f, cancel, ffc)
ff2 := append(funcSlice, funcSlice...)
fmt.Printf("append(%T, %T...) = %T\n", funcSlice, funcSlice, ff2)
// returns type []func() instead of Funcs
FF2 := append(funcs, funcs...)
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcs, FF2)
// returns type []func() instead of Funcs
Ff2 := append(funcs, funcSlice...)
fmt.Printf("append(%T, %T...) = %T\n", funcs, funcSlice, Ff2)
// cannot use cancelSlice (variable of type []context.CancelFunc) as []func() value in argument
// fc2 := append(funcSlice, cancelSlice...)
// fmt.Printf("append(%T, %T...) = %T\n", funcSlice, cancelSlice, fc2)
// cannot use cancels (variable of type Cancels) as []func() value in argument
// FC2 := append(funcs, cancels...)
// fmt.Printf("append(%T, %T...) = %T\n", funcs, cancels, FC2)
rr := append(recvSlice, r)
fmt.Printf("append(%T, %T) = %T\n", recvSlice, r, rr)
rb := append(recvSlice, b)
fmt.Printf("append(%T, %T) = %T\n", recvSlice, b, rb)
RR := append(RecvSlice, R)
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, R, RR)
Rb := append(RecvSlice, b)
fmt.Printf("append(%T, %T) = %T\n", RecvSlice, b, Rb)
rrb := append(recvSlice, r, b)
fmt.Printf("append(%T, %T) = %T\n", recvSlice, b, rrb)
rr2 := append(recvSlice, recvSlice...)
fmt.Printf("append(%T, %T...) = %T\n", recvSlice, recvSlice, rr2)
// cannot use bidiSlice (variable of type []chan int) as []<-chan int value in argument
// rb2 := append(recvSlice, bidiSlice...)
// fmt.Printf("append(%T, %T...) = %T\n", recvSlice, bidiSlice, rb2)
}