Exploring Golang's Range Keyword

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The range keyword is used to iterate over elements in a variety of data structures in Golang. In this challenge, we will explore how to use range with different data structures.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/range("`Range`") subgraph Lab Skills go/range -.-> lab-15417{{"`Exploring Golang's Range Keyword`"}} end

Range

The problem to be solved in this challenge is to demonstrate how to use range with slices, arrays, maps, and strings.

Requirements

To complete this challenge, you will need:

  • Basic knowledge of Golang syntax
  • Golang installed on your machine

Example

$ go run range.go
sum: 9
index: 1
a - > apple
b - > banana
key: a
key: b
0 103
1 111

Summary

In this challenge, we learned how to use range with slices, arrays, maps, and strings in Golang. The range keyword provides a convenient way to iterate over elements in different data structures.

Other Go Tutorials you may like