Skip to content

Instantly share code, notes, and snippets.

@jjylik
Last active March 18, 2020 13:57
Show Gist options
  • Save jjylik/8a35f9ee05acd970d1aa210d2d1af257 to your computer and use it in GitHub Desktop.
Save jjylik/8a35f9ee05acd970d1aa210d2d1af257 to your computer and use it in GitHub Desktop.
Make me cool example go program

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"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment