mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
Some cool code gen stuff
This commit is contained in:
71
main.go
71
main.go
@ -1,5 +1,72 @@
|
||||
//go:generate go run inliner/main.go
|
||||
package main
|
||||
|
||||
func main() {
|
||||
println("Hello there, friend.")
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/bloeys/cogo/cogo"
|
||||
)
|
||||
|
||||
type Coroutine[T any] struct {
|
||||
State int32
|
||||
In *T
|
||||
}
|
||||
|
||||
func (c *Coroutine[T]) Run(f func(in *T)) {
|
||||
f(c.In)
|
||||
}
|
||||
|
||||
var state = 0
|
||||
|
||||
func Wow() {
|
||||
println("wow")
|
||||
}
|
||||
|
||||
func test() {
|
||||
|
||||
cogo.Begin()
|
||||
|
||||
println("hi")
|
||||
println("this is from state_0")
|
||||
cogo.Yield()
|
||||
state = 1
|
||||
|
||||
if 1 > 2 {
|
||||
println("gg")
|
||||
}
|
||||
|
||||
println("Bye")
|
||||
println("this is from state_1")
|
||||
cogo.Yield()
|
||||
state = 2
|
||||
|
||||
cogo.End()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
test()
|
||||
test()
|
||||
test()
|
||||
|
||||
println("Final state:", state)
|
||||
}
|
||||
|
||||
func FileLine() int {
|
||||
_, _, lineNum, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
panic("failed to get line number. Stack trace: " + string(debug.Stack()))
|
||||
}
|
||||
return lineNum
|
||||
}
|
||||
|
||||
func FileLineString() string {
|
||||
_, _, lineNum, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
panic("failed to get line number. Stack trace: " + string(debug.Stack()))
|
||||
}
|
||||
|
||||
return fmt.Sprint(lineNum)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user