Skip to content

Instantly share code, notes, and snippets.

@jmcshane
Last active March 31, 2021 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcshane/b6f23721b38e6554e2ae759e14a94b31 to your computer and use it in GitHub Desktop.
Save jmcshane/b6f23721b38e6554e2ae759e14a94b31 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
target := make(chan error)
count := background(target)
for i := 0; i < count; i++ {
err := <-target
if err != nil {
fmt.Printf("Error output: %s\n", err)
}
}
}
func background(target chan error) int {
for i := 0; i < 2; i++ {
go func(j int) {
var err error
defer func() {
fmt.Printf("j is %d Out err value is: %s\n", j, err)
target <- err
}()
value, err := getError(j)
if err != nil {
fmt.Printf("test: %s err: %s\n", value, err)
}
}(i)
}
return 2
}
func getError(i int) (string, error) {
if i == 1 {
return "a value", fmt.Errorf("This is an error")
}
return "", nil
}
@jmcshane
Copy link
Author

Output:

test: a value err: This is an error
j is 1 Out err value is: This is an error
Error output: This is an error
j is 0 Out err value is: %!s(<nil>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment