添加context
This commit is contained in:
parent
90f417b1c2
commit
7b16829151
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
21
context_demo/contextDemo1.go
Normal file
21
context_demo/contextDemo1.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const shortDuration = 1 * time.Millisecond
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case <-time.After(1 * time.Second):
|
||||
fmt.Println("脑子进煎鱼了")
|
||||
case <-ctx.Done():
|
||||
fmt.Println(ctx.Err())
|
||||
}
|
||||
}
|
27
context_demo/demo2.go
Normal file
27
context_demo/demo2.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
req, err := http.NewRequest("GET", "https://eddycjy.com/", nil)
|
||||
if err != nil {
|
||||
fmt.Printf("http.NewRequest err: %+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(req.Context(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
req = req.WithContext(ctx)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("http.DefaultClient.Do err: %+v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
}
|
23
context_demo/demo3.go
Normal file
23
context_demo/demo3.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type favContextKey string
|
||||
f := func(ctx context.Context, k favContextKey) {
|
||||
if v := ctx.Value(k); v != nil {
|
||||
fmt.Println("found value:", v)
|
||||
return
|
||||
}
|
||||
fmt.Println("key not found:", k)
|
||||
}
|
||||
|
||||
k := favContextKey("脑子进")
|
||||
ctx := context.WithValue(context.Background(), k, "煎鱼")
|
||||
|
||||
f(ctx, k)
|
||||
f(ctx, favContextKey("小咸鱼"))
|
||||
}
|
1
go.sum
1
go.sum
@ -32,6 +32,7 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
|
Loading…
Reference in New Issue
Block a user