Byte arrays and Channels
Hi folks and a Happy new Year! Today, I would like to show you some interesting things you can do with channels. Consider the following simple example. package main import "fmt" func main() { generatedPassword := make(chan int, 100) correctPassword := make(chan int) defer close(generatedPassword) defer close(correctPassword) go passwordIncrement(generatedPassword) go checkPassword(generatedPassword, correctPassword) pass := <-correctPassword fmt.Println(pass) } func checkPassword(input <-chan int, output chan<- int) { for { p := <-input //Introduce lengthy operation here // time.Sleep(time.Second) fmt.Println("Checking p:", p) if p > 100000 { output <- p } } } func passwordIncrement(out chan<- int) { p := 0 for { p++ out <- p } } The premise is as follows. It launches two go routines. One, which generates passwords, and an other which checks for validity. The two routines talk to each other through the channel generatedPassword. That’s the providing connections between them. The channel correctPassword provides output for the checkPassword routine. ...