Skip to content

Instantly share code, notes, and snippets.

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