Measure a rhythm
Before you start this intermediate track
Functions and typed collections. Work with slices, map lookups, and multiple return values.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A rehearsal tool groups a beat count into musical bars containing four beats each. One Go function can return two results. Integer division finds the number of complete bars, and the remainder operator finds the extra beats. The caller receives both results in the same order as the return statement.
package mainimport "fmt"func measure(beats int) (int, int) { return beats / 4, beats % 4}func main() { bars, extra := measure(14) fmt.Printf("%d bars, %d beats\n", bars, extra)}Each mission is a complete independent Go program. Save it as main.go and run go run main.go with Go 1.18 or later. Only the standard fmt package is used.
Track the values line by line. Predict the result before running this example in your own editor.