golang context的WithTimeout实现调用自定义函数超时处理

context的WithTimeout实现调用自定义函数超时处理


func UploadContent(content, objectName string) chan error {
	errCh := make(chan error, 1)
    return errCh
}

func  UploadFromString(content, objectName string) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond * 5000))
    defer cancel()
	select {
	case <- this.UploadContent(content, objectName):
		return 
    case <- ctx.Done():
		fmt.Println(“Upload Oss Timeout"”)
        return 
	}
}

上一篇:如何安全close go 的channel


下一篇:Go语言学习篇06