From 1e39bd83362496e8897cb9017379946105f5a3bc Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 8 Jul 2024 15:37:20 +0800 Subject: [PATCH 1/2] Program.Zero: support types.Signature/Chan --- ssa/expr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssa/expr.go b/ssa/expr.go index 270f7324..881cc0d5 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -127,7 +127,7 @@ func (p Program) Zero(t Type) Expr { default: panic("todo") } - case *types.Pointer: + case *types.Pointer, *types.Signature, *types.Chan: return Expr{llvm.ConstNull(t.ll), t} case *types.Struct: n := u.NumFields() From c5047186ddfd5dba0cbd2ae32592686218cb9c4a Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 8 Jul 2024 15:50:16 +0800 Subject: [PATCH 2/2] demo: ctxcancel --- _demo/ctxcancel/ctx.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 _demo/ctxcancel/ctx.go diff --git a/_demo/ctxcancel/ctx.go b/_demo/ctxcancel/ctx.go new file mode 100644 index 00000000..3b9afc26 --- /dev/null +++ b/_demo/ctxcancel/ctx.go @@ -0,0 +1,39 @@ +package main + +import ( + "context" + "fmt" +) + +func main() { + // gen generates integers in a separate goroutine and + // sends them to the returned channel. + // The callers of gen need to cancel the context once + // they are done consuming generated integers not to leak + // the internal goroutine started by gen. + gen := func(ctx context.Context) <-chan int { + dst := make(chan int) + n := 1 + go func() { + for { + select { + case <-ctx.Done(): + return // returning not to leak the goroutine + case dst <- n: + n++ + } + } + }() + return dst + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // cancel when we are finished consuming integers + + for n := range gen(ctx) { + fmt.Println(n) + if n == 5 { + break + } + } +}