Created
August 14, 2015 21:19
-
-
Save tastywheat/82de8ddbfaee8a7a944c to your computer and use it in GitHub Desktop.
golang yield with struct
This file contains 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