𝙏𝙝𝙚 𝙊𝙡𝙙 𝙉𝙚𝙬 𝙏𝙝𝙞𝙣𝙜
golang co-routine is a source to source compiler which rewrites trival yield statements to monadic style code.
Inspired by wind-js.
Create source files ending with _co.go
/ _co_test.go
.
Build tag //go:build co
required.
Then go generate -tags co ./...
(or run by IDE whatever).
And it is a good idea to switch custom build tag to co
when working in goland or vscode,
so IDE will be happy to index and check your code.
//go:build co
//go:generate go install github.com/goghcrow/go-co/cmd/cogen
//go:generate cogen
package main
import (
. "github.com/goghcrow/go-co"
)
func Fibonacci() Iter[int] {
a, b := 1, 1
for {
Yield(b)
a, b = b, a+b
}
}
func main() {
for n := range Fibonacci() {
if n > 1000 {
println(n)
break
}
}
}
go get github.com/goghcrow/go-co@latest
package main
import (
"github.com/goghcrow/go-co/rewriter"
"github.com/goghcrow/go-loader"
)
func main() {
rewriter.Compile(
"./src",
"./out",
loader.WithLoadTest(),
)
}
Rewrite control flow to monadic func invoking.
- IfStmt
- SwitchStmt
- Fallthrough
- TypeSwitchStmt
- ForStmt
- RangeStmt
- string
- slice
- map
- array
- integer
- channel
- range func
- BlockStmt
- Break / Continue
- Non-Label
- Label
- Goto
- SelectStmt
- DeferStmt