Исходники и презентации
This commit is contained in:
26
lessons/contexts/context_handling/main.go
Normal file
26
lessons/contexts/context_handling/main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Query(string) string
|
||||
|
||||
func DoQeury(qyeryStr string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
|
||||
var resultCh chan string
|
||||
go func() {
|
||||
result := Query(qyeryStr)
|
||||
resultCh <- result
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
case result := <-resultCh:
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user