Created
July 24, 2019 15:45
-
-
Save betandr/bce50fa15302cfd8e263b3d6dc3c0cda to your computer and use it in GitHub Desktop.
Message-passing with channels.
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" | |
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