go get -u github.com/1pkg/golatch
Golatch seamlessly patches go runtime to provide a way to close a chan idempotently + overwrite empty value returned from that closed channel.
| With Golatch | Without Golatch |
|---|---|
package main
import (
"fmt"
"sync"
"github.com/1pkg/golatch"
)
func main() {
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go worker(i, ch, &wg)
}
golatch.Close(ch, 10)
wg.Wait()
}
func worker(i int, ch chan int, wg *sync.WaitGroup) {
v, ok := <- ch // 10, false
if ok || v != 10 {
panic("unreachable") // won't panic
}
fmt.Printf("worker %d chan is closed with value %d\n", i, v)
wg.Done()
} |
package main
import (
"fmt"
"sync"
)
func main() {
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go worker(i, ch, &wg)
}
close(ch)
wg.Wait()
}
func worker(i int, ch chan int, wg *sync.WaitGroup) {
v, ok := <- ch // 0, false
if ok || v != 10 {
panic("unreachable") // will panic
}
fmt.Printf("worker %d chan is closed with value %d\n", i, v)
wg.Done()
} |
Golatch exposes single function Close that idempotently closes provided chan and stores provided value to return as this channel closed value instead of empty value. If provided channel is not a writable channel NotWritableChannel error is returned. If provided value doesn't match underlying channel type ChannelTypeMismatch error is returned.
To cancel the effect of closed value replace call cancel function. Note that provided value won't be automatically collected by GC together with provided channel to remove provided value from the storage call cancel function. Note that after cancelation is called next call to Close will cause a panic close of closed channel. Note that unless cancelation is called next call to Close is safe and won't cause any panic but just update storage value with new provided value. Note that in order to achieve such effect golatch uses package based on bou.ke/monkey to patch all existing channel receive entrypoints:
- direct chan receive
- chan select statement
- reflect chan receive
This makes golatch inherits the same list of restrictions as bou.ke/monkey has. Note that multiselect statement is patched via patch guard, which makes it not thread safe.
Golatch is licensed under the MIT License.
See LICENSE for the full license text.