typeover
curriculum

Curriculum Collections Iteration with range exercise 5 · mcq

Iteration with range

Range over a MAP one-value-form: which one does Go emit, key or value? Compare with exercises 2 and 3 (slice): for x := range slice gives the INDEX. Now MAPs: for x := range m gives the KEY. The same range x syntax means different things by collection type. TypeScript: ``ts for (const k of Object.keys(cache)) { console.log(k); } `` Pick the Go form that prints just the keys.

TypeScript reference
Pick the idiomatic Go translation

About this theme

for i, v := range collection is the workhorse. Over a slice you get (index, value). Over a map you get (key, value). Over a string you get (byteIndex, rune). Over a channel you get just the value, with no index. Use _ to discard either side.