Standard library / runtime/trace
trace.NewTask()
Create a task and derived context. Pass that context to participating goroutines and finish the task once the logical operation completes. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func NewTask(pctx context.Context, taskType string) (ctx context.Context, task *Task)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| pctx | context.Context | Parent context; a task in it becomes the new task’s parent. |
| taskType | string | Low-cardinality classification of the logical task. |
Return Value
(ctx context.Context, task *Task)
Derived context carrying the task and handle for ending it.
Working Example
Example Snippetgo
package main
import ( "bytes" "context" "fmt" "runtime/trace")
func main() { var b bytes.Buffer if err := trace.Start(&b); err != nil { panic(err) } c, t := trace.NewTask(context.Background(), "job") done := make(chan struct{}) go func() { trace.WithRegion(c, "work", func() {}); close(done) }() <-done t.End() trace.Stop() fmt.Println(b.Len() > 0)}