Created
December 5, 2018 13:23
-
-
Save islishude/ce18eedb35bf08934ffdec7fed870317 to your computer and use it in GitHub Desktop.
Go: 使用 Channel 处理多值返回
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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