Go-context的那些事
程序员文章站
2022-05-05 12:30:08
...
- Background()
- WithCancel()
- WithDeadline()
- WithTimeout()
- WithValue()
上代码, 自己品:
package main
import (
"context"
"fmt"
"sync/atomic"
)
func main() {
const count int32 = 10
var num int32
ctx, cancelFunc := context.WithCancel(context.Background())
for i := int32(0); i < count; i++ {
go func(i int32) {
defer func() {
atomic.AddInt32(&num, 1)
if atomic.LoadInt32(&num) == count {
cancelFunc()
}
}()
fmt.Println(atomic.LoadInt32(&num), i)
}(i)
}
<-ctx.Done()
err := ctx.Err()
fmt.Println(err)
fmt.Println("Done")
}
/*
0 9
1 7
2 8
3 0
0 4
5 3
6 5
4 2
0 1
0 6
context canceled
Done
*/
package main
import (
"context"
"fmt"
"sync/atomic"
"time"
)
func main() {
const count int32 = 10
var num int32
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Second*2))
for i := int32(0); i < count; i++ {
go func(i int32) {
defer func() {
atomic.AddInt32(&num, 1)
if atomic.LoadInt32(&num) == count {
cancelFunc()
}
}()
time.Sleep(time.Second * time.Duration(i))
fmt.Println(atomic.LoadInt32(&num), i)
}(i)
}
<-ctx.Done()
err := ctx.Err()
fmt.Println(err)
fmt.Println("Done")
}
/*
0 0
1 1
2 2
context deadline exceeded
Done
*/
package main
import (
"context"
"fmt"
"sync/atomic"
"time"
)
func main() {
const count int32 = 10
var num int32
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*2)
for i := int32(0); i < count; i++ {
go func(i int32) {
defer func() {
atomic.AddInt32(&num, 1)
if atomic.LoadInt32(&num) == count {
cancelFunc()
}
}()
time.Sleep(time.Second * time.Duration(i))
fmt.Println(atomic.LoadInt32(&num), i)
}(i)
}
<-ctx.Done()
err := ctx.Err()
fmt.Println(err)
fmt.Println("Done")
}
/*
0 0
1 1
context deadline exceeded
2 2
Done
*/
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx := context.WithValue(context.Background(), "context key", "context value")
go func() {
fmt.Println(ctx.Value("context key"))
}()
fmt.Println("Done")
time.Sleep(time.Second)
}
/*
Done
context value
*/
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx1 := context.WithValue(context.Background(), "key1", "val 1")
ctx2 := context.WithValue(ctx1, "key2", "val 2")
ctx3 := context.WithValue(ctx2, "key2", "val 22222")
ctx4 := context.WithValue(ctx3, "key4", "val 4")
ctx, cancelFunc := context.WithTimeout(ctx4, time.Second*2)
go func() {
defer cancelFunc()
fmt.Println(ctx.Value("key1"))
fmt.Println(ctx.Value("key2"))
fmt.Println(ctx.Value("key4"))
}()
<-ctx.Done()
fmt.Println("Done")
}
/*
val 1
val 22222
val 4
Done
*/
上一篇: Linux新手最容易跳进哪几个坑?