Chal1001 - Golang


A GoRoutines Problem

Side columns

Modify this code as commented.

//=============   version 1
func generateKey() int {
	fmt.Println("Generating key")
	// Super-secret algorithm!
	keys := []int{3, 5, 7, 11}
	key := keys[rand.Intn(len(keys))]
	// It's kinda slow!
	time.Sleep(3 * time.Second)
	fmt.Println("Done generating")
	return key
}

func main() {
	rand.Seed(time.Now().Unix())
	for i := 0; i < 3; i++ {
		go generateKey()
	}
	fmt.Println("All done!")
}

ANSWER

package main

import (
	"fmt"
	"math/rand"
	"time"
)

// Update this function to accept a channel parameter,
// and remove the return value.
func generateKey(channel chan int) {
	fmt.Println("Generating key")
	keys := []int{3, 5, 7, 11}
	key := keys[rand.Intn(len(keys))]
	time.Sleep(3 * time.Second)
	fmt.Println("Done generating")
	// Write the key to the channel instead of returning.
	channel <- key
}

func main() {
	rand.Seed(time.Now().Unix())
	// Create a channel.
	channel := make(chan int)
	// Create 3 more goroutines.
	for i := 0; i < 3; i++ {
		go generateKey(channel)
	}
	// Read and print keys from the channel.
	// This also causes the program to wait until 3
	// keys have been read.

	fmt.Println("============  start")
	for i := 0; i < 3; i++ {
		fmt.Println("channel ", i)
		fmt.Println(<-channel)
	}
	fmt.Println("All done!")
}

See also


vyDiagnostics here


title: Chal1001 - Golang

Type: chal