[dev.go2go] examples: update generic examples to use latest syntax
Change-Id: Ib41cb92dc0c3a3446a9a349ccdda88968cd60a27
Reviewed-on: https://go-review.googlesource.com/c/playground/+/251937
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/examples/hello.txt b/examples/hello.txt
index 2694467..f2036a7 100644
--- a/examples/hello.txt
+++ b/examples/hello.txt
@@ -4,12 +4,14 @@
"fmt"
)
-// The playground now supports parentheses or square brackets (only one at
-// a time) for generic type and function declarations and instantiations.
-// By default, parentheses are expected. To switch to square brackets,
-// the first generic declaration in the source must use square brackets.
+// The playground now uses square brackets for type parameters. Otherwise,
+// the syntax of type parameter lists matches the one of regular parameter
+// lists except that all type parameters must have a name, and the type
+// parameter list cannot be empty. The predeclared identifier "any" may be
+// used in the position of a type parameter constraint (and only there);
+// it indicates that there are no constraints.
-func Print[type T](s []T) {
+func Print[T any](s []T) {
for _, v := range s {
fmt.Print(v)
}
diff --git a/examples/min.txt b/examples/min.txt
index 81f8af9..4f56795 100644
--- a/examples/min.txt
+++ b/examples/min.txt
@@ -8,7 +8,7 @@
type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64
}
-func min(type T numeric)(a, b T) T {
+func min[T numeric](a, b T) T {
if a < b {
return a
}
diff --git a/examples/queue.txt b/examples/queue.txt
index 69ac457..058adc3 100644
--- a/examples/queue.txt
+++ b/examples/queue.txt
@@ -4,13 +4,13 @@
"fmt"
)
-type queue(type T) []T
+type queue[T any] []T
-func (q *queue(T)) enqueue(v T) {
+func (q *queue[T]) enqueue(v T) {
*q = append(*q, v)
}
-func (q *queue(T)) dequeue() (T, bool) {
+func (q *queue[T]) dequeue() (T, bool) {
if len(*q) == 0 {
var zero T
return zero, false
@@ -21,7 +21,7 @@
}
func main() {
- q := new(queue(int))
+ q := new(queue[int])
q.enqueue(5)
q.enqueue(6)
fmt.Println(q)