Skip to content

Instantly share code, notes, and snippets.

@sidchilling
Created March 25, 2021 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidchilling/cee84a799bc060b413f1fadb3dad3c54 to your computer and use it in GitHub Desktop.
Save sidchilling/cee84a799bc060b413f1fadb3dad3c54 to your computer and use it in GitHub Desktop.
Go Routing question
package main
// Below is a function `sum()` that takes a number, runs a loop to add, and returns the result.
// In the main function, we call `sum()` once with a small number (e.g. 1000) and once
// with a large number (e.g. 1000000000) and prints the result. After that, the main()
// prints the string "All Done!"
// Your job is to convert the main() so that we call sum() concurrently, but wait for all the invocations
// to sum() to complete before printing "All Done".
import (
"fmt"
)
func sum(num int) int {
var res int
for i := 1; i <= num; i++ {
res += 1
}
return res
}
func main() {
fmt.Println(sum(1000000000))
fmt.Println(sum(1000))
fmt.Println("All Done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment