Skip to content

Instantly share code, notes, and snippets.

@islishude
Created December 5, 2018 13:23
Show Gist options
  • Save islishude/ce18eedb35bf08934ffdec7fed870317 to your computer and use it in GitHub Desktop.
Save islishude/ce18eedb35bf08934ffdec7fed870317 to your computer and use it in GitHub Desktop.
Go: 使用 Channel 处理多值返回
package main
import (
"log"
"time"
)
func main() {
log.Println("start")
num, err := func() (int, error) {
n := make(chan int, 1)
e := make(chan error, 1)
go func() {
defer close(n)
defer close(e)
// Stand-in for a IO operation
time.Sleep(time.Second * 3)
n <- 100
e <- nil
}()
return <-n, <-e
}()
log.Println(num, err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment