Skip to content

Instantly share code, notes, and snippets.

@betandr
Created July 24, 2019 15:45
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 betandr/bce50fa15302cfd8e263b3d6dc3c0cda to your computer and use it in GitHub Desktop.
Save betandr/bce50fa15302cfd8e263b3d6dc3c0cda to your computer and use it in GitHub Desktop.
Message-passing with channels.
package main
import "fmt"
type Greeter struct{}
func (p *Greeter) greet(s string) {
fmt.Printf("Hello, %s!\n", s)
}
func CreateGreeter(ch chan string) *Greeter {
greeter := &Greeter{}
go func(ch chan string) {
for s := range ch {
greeter.greet(s)
}
}(ch)
return greeter
}
func main() {
ch := make(chan string)
CreateGreeter(ch)
ch <- "World"
// wait for input
var s string
fmt.Scanln(&s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment