Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context: WithCancel example is not explaining cancel func #17534

Closed
rakyll opened this issue Oct 21, 2016 · 1 comment
Closed

context: WithCancel example is not explaining cancel func #17534

rakyll opened this issue Oct 21, 2016 · 1 comment
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rakyll
Copy link
Contributor

rakyll commented Oct 21, 2016

As a user, I'd expect WithCancel example at https://tip.golang.org/pkg/context/#example_WithCancel to explain specific cancellation of the context by calling the cancel func. The example doesn't even require a cancellable context, see https://play.golang.org/p/4hMwLTCVaU.

Is this example trying to achieve something similar to what's below?

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    count := func(ctx context.Context, dst chan<- int) {
        n := 1
        for {
            select {
            case dst <- n:
                n++
            case <-ctx.Done():
                fmt.Println("not leaking")
                return
            }
        }
    }

    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    ints := make(chan int)
    go count(ctx, ints)
    for n := range ints {
        fmt.Println(n)
        if n == 5 {
            cancel()
            break
        }
    }

    time.Sleep(time.Minute)
}

/cc @bradfitz

@rakyll rakyll added this to the Go1.8 milestone Oct 21, 2016
@quentinmit quentinmit added Documentation NeedsFix The path to resolution is known, but the work has not been done. labels Oct 21, 2016
@gopherbot
Copy link

CL https://golang.org/cl/32017 mentions this issue.

@golang golang locked and limited conversation to collaborators Oct 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants