-
-
Save shuding/71829fc196d73d1fbecdca05d93fa51d to your computer and use it in GitHub Desktop.
golang yield with struct
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 "fmt" | |
import "time" | |
type person struct { | |
name string | |
} | |
func (p *person) speak () { | |
fmt.Println(p.name) | |
} | |
func NewPerson (name string) person { | |
return person{name} | |
} | |
func dowork () chan string { | |
yield := make(chan string) | |
go func () { | |
time.Sleep(time.Second * 5) | |
yield <- "work complete" | |
}() | |
return yield | |
} | |
func predowork () { | |
foo := dowork() | |
bar := <-foo | |
fmt.Println(bar) | |
} | |
func main () { | |
go predowork() | |
brian := NewPerson("brian") | |
law := NewPerson("law") | |
brian.speak() | |
law.speak() | |
fmt.Println("ended") | |
var input string | |
fmt.Scanln(&input) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment