How to return at once in function when the context is cancel in GoLang?

huangapple go评论78阅读模式
英文:

How to return at once in function when the context is cancel in GoLang?

问题

package main

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

func main() {
	ctx := context.Background()
	c, fn := context.WithCancel(ctx)
	go doSth(c)
	time.Sleep(1 * time.Second)
	fn()
	time.Sleep(10 * time.Second)
}

func doSth(ctx context.Context) {
	fmt.Println("doing")
	time.Sleep(2 * time.Second)
	fmt.Println("still doing")
	select {
	case <-ctx.Done():
		fmt.Println("cancel")
		return
	}
}

输出:

doing
cancel

我不知道如何使这个doSth函数在接收到取消的上下文时返回。

换句话说,我希望这个函数的输出是:

输出:

doing
cancel
英文:
package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	ctx := context.Background()
	c, fn := context.WithCancel(ctx)
	go doSth(c)
	time.Sleep(1 * time.Second)
	fn()
	time.Sleep(10 * time.Second)
}

func doSth(ctx context.Context) {
    fmt.Println(&quot;doing&quot;)
    time.Sleep(2 * time.Second)
	fmt.Println(&quot;still doing&quot;)
	select {
	case &lt;-ctx.Done():
		fmt.Println(&quot;cancel&quot;)
		return
	}
}

OUTPUT:

doing
still doing
cancel

I don't know how to make this doSth function return when the context it get is canncel.

In another word, I want the output of this function is:

OUTPUT:

doing
cancel

答案1

得分: 0

你可以使用一个定时器,在给定的时间后通过一个通道发送一条消息。这样可以将其添加到select语句中。

func main() {
    ctx := context.Background()
    c, fn := context.WithCancel(ctx)
    go doSth(c)
    time.Sleep(1 * time.Second)
    fn()
    time.Sleep(10 * time.Second)
}

func doSth(ctx context.Context) {
    fmt.Println("doing")
    timer := time.NewTimer(2 * time.Second)
    select {
    case <-timer.C:
        fmt.Println("still doing")
    case <-ctx.Done():
        fmt.Println("cancel")
    }
}
英文:

You can use a timer, which will send a message over a channel after the given duration. This allows you to add it in the select.

func main() {
	ctx := context.Background()
	c, fn := context.WithCancel(ctx)
	go doSth(c)
	time.Sleep(1 * time.Second)
	fn()
	time.Sleep(10 * time.Second)
}

func doSth(ctx context.Context) {
	fmt.Println(&quot;doing&quot;)
	timer := time.NewTimer(2 * time.Second)
	select {
	case &lt;-timer.C:
		fmt.Println(&quot;still doing&quot;)
	case &lt;-ctx.Done():
		fmt.Println(&quot;cancel&quot;)
	}
}

huangapple
  • 本文由 发表于 2021年12月4日 21:25:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/70226015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定