Skip to content

Commit 75b5322

Browse files
committed
all: upgrade and move from go2go to gotip
1 parent a66aaf9 commit 75b5322

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1822
-5433
lines changed

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
# go2generics
1+
# go2generics [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/go2generics)](https://pkg.go.dev/golang.design/x/go2generics) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/go2generics)
22

3-
This repository demonstrates a big chunk of demos and researches about Go 2 generics.
3+
A chunk of demos and research regarding Go 2 generics (type parameters).
44

55
## Getting started
66

77
There are several documents for you to getting familiar with generics:
88

99
- [A Summary of Go Generics Research](./generics.md)
10-
- Changkun Ou. Go 2 Generics: Constrained Type Parameters. https://changkun.de/s/go2generics/.
10+
- Changkun Ou. Go 2 Generics: Type Parameters. https://changkun.de/s/go2generics/.
1111

1212
## Code examples
1313

14-
You can use `go2go` to build all code examples in this repository.
14+
You can use the latest Go tip version to build all code examples in this repository.
1515

16-
### Using `go2go`
16+
```sh
17+
$ git clone https://github.com/golang/go && cd src && ./all.bash
1718

18-
1. Clone `go` repository
19-
20-
2. Checkout `go2go` branch and build the go toolchain
21-
22-
3. Place the repository in `GO2PATH`. The `go2generics` repository path should looks like the follows:
23-
24-
```
25-
$GO2PATH/src/github.com/changkun/go2generics
19+
$ ../bin/go test -gcflags=all=-G=3 -v ./...
2620
```
2721

28-
Use `go tool go2go` for executing and testing the demos.
22+
The `-gcflags=all=-G=3` is the key compiler flag to run Go code with type parameters.
2923

3024
## Licnese
3125

32-
BSD-2-Clause
26+
BSD-2-Clause
3327

34-
2020 © [Changkun Ou](https://changkun.de)
28+
Copyright © 2020-2021 [Changkun Ou](https://changkun.de)

chans/chans.go

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
1-
// Code generated by go2go; DO NOT EDIT.
1+
// Copyright 2020 Changkun Ou. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
24

3-
4-
//line chans.go2:5
55
package chans
66

7-
//line chans.go2:5
8-
import (
9-
//line chans.go2:5
10-
"math/rand"
11-
//line chans.go2:5
12-
"runtime"
13-
//line chans.go2:5
14-
"sync"
15-
//line chans.go2:5
16-
"testing"
17-
//line chans.go2:5
18-
)
19-
20-
//line chans.go2:5
21-
type Importable୦ int
22-
23-
//line chans.go2:5
24-
var _ = rand.ExpFloat64
25-
//line chans.go2:5
26-
var _ = runtime.BlockProfile
27-
28-
//line chans.go2:5
29-
type _ sync.Cond
30-
31-
//line chans.go2:5
32-
var _ = testing.AllocsPerRun
7+
import "runtime"
8+
9+
// Ranger returns a Sender and a Receiver. The Receiver provides a
10+
// Next method to retrieve values. The Sender provides a Send method
11+
// to send values and a Close method to stop sending values. The Next
12+
// method indicates when the Sender has been closed, and the Send
13+
// method indicates when the Receiver has been freed.
14+
//
15+
// This is a convenient way to exit a goroutine sending values when
16+
// the receiver stops reading them.
17+
func Ranger[T any]() (*Sender[T], *Receiver[T]) {
18+
c := make(chan T)
19+
d := make(chan bool)
20+
s := &Sender[T]{values: c, done: d}
21+
r := &Receiver[T]{values: c, done: d}
22+
runtime.SetFinalizer(r, r.finalize)
23+
return s, r
24+
}
25+
26+
// A sender is used to send values to a Receiver.
27+
type Sender[T any] struct {
28+
values chan<- T
29+
done <-chan bool
30+
}
31+
32+
// Send sends a value to the receiver. It returns whether any more
33+
// values may be sent; if it returns false the value was not sent.
34+
func (s *Sender[T]) Send(v T) bool {
35+
select {
36+
case s.values <- v:
37+
return true
38+
case <-s.done:
39+
return false
40+
}
41+
}
42+
43+
// Close tells the receiver that no more values will arrive.
44+
// After Close is called, the Sender may no longer be used.
45+
func (s *Sender[T]) Close() {
46+
close(s.values)
47+
}
48+
49+
// A Receiver receives values from a Sender.
50+
type Receiver[T any] struct {
51+
values <-chan T
52+
done chan<- bool
53+
}
54+
55+
// Next returns the next value from the channel. The bool result
56+
// indicates whether the value is valid, or whether the Sender has
57+
// been closed and no more values will be received.
58+
func (r *Receiver[T]) Next() (T, bool) {
59+
v, ok := <-r.values
60+
return v, ok
61+
}
62+
63+
// finalize is a finalizer for the receiver.
64+
func (r *Receiver[T]) finalize() {
65+
close(r.done)
66+
}

chans/chans.go2

Lines changed: 0 additions & 66 deletions
This file was deleted.

chans/lb.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1-
// Code generated by go2go; DO NOT EDIT.
1+
// Copyright 2020 Changkun Ou. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
24

3-
4-
//line lb.go2:5
55
package chans
66

7-
//line lb.go2:5
87
import (
9-
//line lb.go2:5
10-
"math/rand"
11-
//line lb.go2:5
12-
"runtime"
13-
//line lb.go2:5
14-
"sync"
15-
//line lb.go2:5
16-
"testing"
17-
//line lb.go2:5
8+
"sync"
9+
"math/rand"
1810
)
1911

20-
//line lb.go2:5
21-
var _ = rand.ExpFloat64
22-
//line lb.go2:5
23-
var _ = runtime.BlockProfile
12+
// Fanin implements a generic fan-in for variadic channels.
13+
func Fanin[T any](chans ...<-chan T) <-chan T {
14+
buf := 0
15+
for _, ch := range chans {
16+
if len(ch) > buf { buf = len(ch) }
17+
}
18+
out := make(chan T, buf)
19+
wg := sync.WaitGroup{}
20+
wg.Add(len(chans))
21+
for _, ch := range chans {
22+
go func(ch <-chan T) {
23+
for v := range ch { out <- v }
24+
wg.Done()
25+
}(ch)
26+
}
27+
go func() {
28+
wg.Wait()
29+
close(out)
30+
}()
31+
return out
32+
}
2433

25-
//line lb.go2:5
26-
type _ sync.Cond
34+
// Fanout implements a generic fan-out for variadic channels
35+
func Fanout[T any](randomizer func(max int) int, In <-chan T, Outs ...chan T) {
36+
l := len(Outs)
37+
for v := range In {
38+
i := randomizer(l)
39+
if i < 0 || i > l { i = rand.Intn(l) }
40+
go func(v T) { Outs[i] <- v }(v)
41+
}
42+
}
2743

28-
//line lb.go2:5
29-
var _ = testing.AllocsPerRun
44+
func LB[T any](randomizer func(max int) int, ins []<-chan T, outs []chan T) {
45+
Fanout(randomizer, Fanin(ins...), outs...)
46+
}

chans/lb.go2

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)