Skip to content

Commit 2092e6b

Browse files
changed examples to use square bracket syntax
1 parent c35b9f1 commit 2092e6b

File tree

13 files changed

+48
-49
lines changed

13 files changed

+48
-49
lines changed

topics/go/generics/01-basic/generic.go2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func printReflect(v interface{}) {
7272
// implementations that we started with and is easier to read than the reflect
7373
// implementation.
7474

75-
func printGeneric(type T)(list []T) {
75+
func printGeneric[type T](slice []T) {
7676
fmt.Print("Generic: ")
77-
for _, v := range list {
77+
for _, v := range slice {
7878
fmt.Print(v, " ")
7979
}
8080
fmt.Print("\n")

topics/go/generics/02-named-type/generic.go2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func (v vectorInterface) push(value interface{}) vectorInterface {
4343
// accepts any value of type T determined at compile time. This value needs to
4444
// match the type used in the construction of the generics type.
4545

46-
type vectorGeneric(type T) []T
46+
type vectorGeneric[type T] []T
4747

48-
func (v vectorGeneric(T)) push(value T) vectorGeneric(T) {
48+
func (v vectorGeneric[T]) push(value T) vectorGeneric[T] {
4949
v = append(v, value)
5050
return v
5151
}
@@ -70,12 +70,12 @@ func main() {
7070
vItf = vItf.push("B")
7171
fmt.Println("vectorInterface:", vItf)
7272

73-
var vGenInt vectorGeneric(int)
73+
var vGenInt vectorGeneric[int]
7474
vGenInt = vGenInt.push(10)
7575
vGenInt = vGenInt.push(20)
7676
fmt.Println("vectorGeneric:", vGenInt)
7777

78-
var vGenStr vectorGeneric(string)
78+
var vGenStr vectorGeneric[string]
7979
vGenStr = vGenStr.push("A")
8080
vGenStr = vGenStr.push("B")
8181
fmt.Println("vectorGeneric:", vGenStr)

topics/go/generics/03-behavior-constraint/generic.go2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ func stringifyReflect(v interface{}) []string {
108108
// applied to allow the compiler to know the value of type T passed will have
109109
// a String method.
110110

111-
func stringifyGeneric(type T fmt.Stringer)(list []T) []string {
112-
ret := make([]string, 0, len(list))
113-
for _, value := range list {
111+
func stringifyGeneric[type T fmt.Stringer](slice []T) []string {
112+
ret := make([]string, 0, len(slice))
113+
for _, value := range slice {
114114
ret = append(ret, value.String())
115115
}
116116
return ret

topics/go/generics/04-type_constraint/generic.go2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type addOnly interface {
1313
type string, int, int8, int16, int32, int64, float64
1414
}
1515

16-
func Add(type T addOnly)(v1 T, v2 T) T {
16+
func Add[type T addOnly](v1 T, v2 T) T {
1717
return v1 + v2
1818
}
1919

@@ -24,7 +24,7 @@ func Add(type T addOnly)(v1 T, v2 T) T {
2424
// comparable constraint accepts as a type argument any comparable type. It
2525
// permits the use of == and != with values of that type parameter.
2626

27-
func index(type T comparable)(list []T, find T) int {
27+
func index[type T comparable](list []T, find T) int {
2828
for i, v := range list {
2929
if v == find {
3030
return i
@@ -67,12 +67,12 @@ func (f food) match(v food) bool {
6767
// Note: The type list inside the interface is not needed for match to work.
6868
// I'm trying to show how the type list and behavior can be combined.
6969

70-
type matcher(type T) interface {
70+
type matcher[type T] interface {
7171
type person, food
7272
match(v T) bool
7373
}
7474

75-
func match(type T matcher)(list []T, find T) int {
75+
func match[type T matcher](list []T, find T) int {
7676
for i, v := range list {
7777
if v.match(find) {
7878
return i

topics/go/generics/05-multi-type-params/generic.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// merged with the same index position of type V into a single string. This code
1212
// shows how the type list can contain more than just one generic type.
1313

14-
func Print(type L interface{}, V fmt.Stringer)(labels []L, vals []V) {
14+
func Print[type L interface{}, V fmt.Stringer](labels []L, vals []V) {
1515
for i, v := range vals {
1616
fmt.Println(labels[i], v.String())
1717
}

topics/go/generics/06-returns/generic.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type entities interface {
106106
type User, Customer
107107
}
108108

109-
func Insert(type T entities)(db *sql.DB, entity T, query string, args ...interface{}) (T, error) {
109+
func Insert[type T entities](db *sql.DB, entity T, query string, args ...interface{}) (T, error) {
110110
var zero T
111111

112112
result, err := ExecuteQuery(query, args...)

topics/go/generics/07-link-list/generic.go2

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ type scalarOnly interface {
2121
type int, int8, int16, int32, int64, string
2222
}
2323

24-
type node(type T scalarOnly) struct {
24+
type node[type T scalarOnly] struct {
2525
Data T
26-
next *node(T)
27-
prev *node(T)
26+
next *node[T]
27+
prev *node[T]
2828
}
2929

30-
type list(type T scalarOnly) struct {
30+
type list[type T scalarOnly] struct {
3131
Count int
32-
first *node(T)
33-
last *node(T)
32+
first *node[T]
33+
last *node[T]
3434
}
3535

36-
func (l *list(T)) add(data T) *node(T) {
37-
n := node(T) {
36+
func (l *list[T]) add(data T) *node[T] {
37+
n := node[T] {
3838
Data: data,
3939
prev: l.last,
4040
}
@@ -51,9 +51,9 @@ func (l *list(T)) add(data T) *node(T) {
5151

5252
// =============================================================================
5353

54-
type op(type T scalarOnly) func(n *node(T)) error
54+
type op[type T scalarOnly] func(n *node[T]) error
5555

56-
func (l *list(T)) operate(f op(T)) error {
56+
func (l *list[T]) operate(f op[T]) error {
5757
n := l.first
5858
for n != nil {
5959
if err := f(n); err != nil {
@@ -62,5 +62,4 @@ func (l *list(T)) operate(f op(T)) error {
6262
n = n.next
6363
}
6464
return nil
65-
}
66-
65+
}

topics/go/generics/08-hash-table/generic.go2

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
hashFunc2 := func(key int, buckets int) int {
2222
return key % buckets
2323
}
24-
table2 := New(int, string)(buckets, hashFunc2)
24+
table2 := New(/*key*/ int, /*value*/ string)(buckets, hashFunc2)
2525

2626
words := []string{"foo", "bar", "baz"}
2727
for i, word := range words {
@@ -40,28 +40,28 @@ func main() {
4040

4141
// =============================================================================
4242

43-
type hashFunc(type K) func(key K, buckets int) int
43+
type hashFunc[type K] func(key K, buckets int) int
4444

45-
type keyValuePair(type K, V interface{}) struct {
45+
type keyValuePair[type K, V interface{}] struct {
4646
Key K
4747
Value V
4848
}
4949

50-
type Table(type K comparable, V interface{}) struct {
51-
hashFunc hashFunc(K)
50+
type Table[type K comparable, V interface{}] struct {
51+
hashFunc hashFunc[K]
5252
buckets int
53-
table [][]keyValuePair(K, V)
53+
table [][]keyValuePair[K, V]
5454
}
5555

56-
func New(type K comparable, V interface{})(buckets int, hf hashFunc(K)) *Table(K, V) {
57-
return &Table(K, V){
56+
func New[type K comparable, V interface{}](buckets int, hf hashFunc[K]) *Table[K, V] {
57+
return &Table[K, V]{
5858
hashFunc: hf,
5959
buckets: buckets,
60-
table: make([][](keyValuePair(K, V)), buckets),
60+
table: make([][](keyValuePair[K, V]), buckets),
6161
}
6262
}
6363

64-
func (t *Table(K, V)) Insert(key K, value V) {
64+
func (t *Table[K, V]) Insert(key K, value V) {
6565
bucket := t.hashFunc(key, t.buckets)
6666
for idx, kvp := range t.table[bucket] {
6767
if key == kvp.Key {
@@ -70,14 +70,14 @@ func (t *Table(K, V)) Insert(key K, value V) {
7070
}
7171
}
7272

73-
kvp := keyValuePair(K, V){
73+
kvp := keyValuePair[K, V]{
7474
Key: key,
7575
Value: value,
7676
}
7777
t.table[bucket] = append(t.table[bucket], kvp)
7878
}
7979

80-
func (t *Table(K, V)) Get(key K) (V, bool) {
80+
func (t *Table[K, V]) Get(key K) (V, bool) {
8181
bucket := t.hashFunc(key, t.buckets)
8282
for idx, kvp := range t.table[bucket] {
8383
if key == kvp.Key {

topics/go/generics/09-slice-constraints/generic.go2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import (
1212
// Slice defines a constraint that the data is a slice of some type T to be
1313
// determined at compile time.
1414

15-
type operateFunc(type T) func(t T) T
15+
type operateFunc[type T] func(t T) T
1616

17-
type Slice(type T) interface {
17+
type Slice[type T] interface {
1818
type []T
1919
}
2020

2121
// When it is important that the slice being passed in is exactly the same
2222
// as the slice being returned, use a slice contraint. This ensures that the
2323
// result slice S is the same as the incoming slice S.
2424

25-
func operate(type S Slice(T), T interface{})(slice S, fn operateFunc(T)) S {
25+
func operate[type S Slice[T], T interface{}](slice S, fn operateFunc[T]) S {
2626
ret := make(S, len(slice))
2727
for i, v := range slice {
2828
ret[i] = fn(v)
@@ -37,7 +37,7 @@ func operate(type S Slice(T), T interface{})(slice S, fn operateFunc(T)) S {
3737
// case not a slice of Numbers, but a slice of integers. This is not the case
3838
// with operate function above.
3939

40-
func operate2(type T)(slice []T, fn operateFunc(T)) []T {
40+
func operate2[type T](slice []T, fn operateFunc[T]) []T {
4141
ret := make([]T, len(slice))
4242
for i, v := range slice {
4343
ret[i] = fn(v)

topics/go/generics/10-channels/generic.go2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// doWork will execute a work function in a goroutine and return a channel of
1414
// type Result back to the caller.
1515

16-
func doWork(type Result)(ctx context.Context, work func(context.Context) Result) chan Result {
16+
func doWork[type Result](ctx context.Context, work func(context.Context) Result) chan Result {
1717
ch := make(chan Result, 1)
1818

1919
go func() {
@@ -30,7 +30,7 @@ func doWork(type Result)(ctx context.Context, work func(context.Context) Result)
3030
// channel of type Input back to the caller. Once input is received by any
3131
// given goroutine, the work function is executed and the Result is displayed.
3232

33-
func poolWork(type Input, Result)(wg *sync.WaitGroup, size int, work func(input Input) Result) chan Input {
33+
func poolWork[type Input, Result](wg *sync.WaitGroup, size int, work func(input Input) Result) chan Input {
3434
ch := make(chan Input)
3535

3636
for i := 0; i < size; i++ {

topics/go/generics/11-swapping/generic.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func swapInterface(v1 interface{}, v2 interface{}) (interface{}, interface{}) {
3636
// implementation has the advanatge that concrete types are being used and only
3737
// data of the same type can be swapped. The caller doesn't require type assertions.
3838

39-
func swapGeneric(type T)(v1 T, v2 T) (T, T) {
39+
func swapGeneric[type T](v1 T, v2 T) (T, T) {
4040
v2, v1 = v1, v2
4141
return v1, v2
4242
}

topics/go/generics/omissions/01-literal-functions/generic.go2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "context"
55
// Compiler Error: function type must have no type parameters
66

77
func example1() {
8-
go func(type T)(v T) {
8+
go func[type T](v T) {
99

1010
// Write some code here.
1111

@@ -17,7 +17,7 @@ func example1() {
1717
// There's no need to make this literal function generic.
1818

1919
func example2() {
20-
f := func(type Result)(ctx context.Context) Result {
20+
f := func[type Result](ctx context.Context) Result {
2121

2222
// Write some code here.
2323

topics/go/generics/omissions/02-methods/generic.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type value struct {
66
s string
77
}
88

9-
func (v value) Method(type Input, Result)(i Input) Result {
9+
func (v value) Method[type Input, Result](i Input) Result {
1010

1111
// Write some code here.
1212
}

0 commit comments

Comments
 (0)