Download and install go
Create a folder for the main.go file
Put main.go there && cd there
go build -o coolmaker
./coolmaker name1 name2
or alternatively just go run main.go name1 name2
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"time" | |
) | |
func coolMaker(s string, coolChannel chan string) { | |
time.Sleep(time.Millisecond * time.Duration((100 * len(s)))) | |
coolChannel <- s + " is 🆒!" | |
} | |
func createCoolPeople(makeCool []string) chan string { | |
coolChannel := make(chan string, 2) | |
for _, cool := range makeCool { | |
go coolMaker(cool, coolChannel) | |
} | |
return coolChannel | |
} | |
func sumCoolPeople(coolChannel chan string, allTheCoolPeople chan []string, numberOfPeople int) { | |
var coolPeople []string | |
for message := range coolChannel { | |
coolPeople = append(coolPeople, message) | |
if len(coolPeople) == numberOfPeople { | |
close(coolChannel) | |
} | |
} | |
allTheCoolPeople <- coolPeople | |
} | |
func main() { | |
makeCool := os.Args[1:] | |
allTheCoolPeople := make(chan []string) | |
coolChannel := createCoolPeople(makeCool) | |
go sumCoolPeople(coolChannel, allTheCoolPeople, len(makeCool)) | |
result := <-allTheCoolPeople | |
fmt.Println(strings.Join(result, "\n")) | |
} |