Go Range

Updated:2021-07-06
Share:

The range keyword is used to iterate over elements of an slice, array, map or channel.

range over Arrays and Slices

  • Use one variable to get the index of an element.
  • Use two varaibles to get both index and the value of an element.

range over Maps

  • Use one variable to get the key of an element.
  • Use two varaibles to get both key and the value of an element.

range over Channels

  • Use one variable to get the value of an element.

How to use range?

range is used with for loop, as follows:

package main

import "fmt"

func main() {
   // array of int
   intArray := [..]int{0,1,2,3,4,5,6,7,8} 
   
   // print all array items
   for i:= range ints {
      fmt.Println("Array item:", i, "=",intArray[i])
   }
   
   // slice of int
   intSlice := []int{0,1,2,3,4,5,6,7,8} 
   
   // print all items in the slice similar to printing an array
   for i:= range intSlice {
      fmt.Println("intSlice item:", i, "=",intSlice[i])
   }
   
   // map
   keywords := map[string] string {"golang":"learn-go","c":"learn-c","home":"coderme.com"}
   
   // print using keys only 
   for k := range keywords {
      fmt.Println("Keyword of", k,"is", keywords[k])
   }
   
   // you can use two arguments as k, v
   for k, v := range keywords {
      fmt.Println("Keyword for", k, "=", v)
   }
   

   // creating a channel
   ch := make(chan int, 5)
   
   // adding items to channel
   for c := 0; c < 5; c++ {
       ch <- c
   }
   
   // print all items in a channel
   for v := range ch {
		fmt.Println(v)
   }
   
   
}

Read also

Go Strings

Strings, which are widely used in Go programming, are a readonly slice of bytes. In the Go programming language, strings are slices. Go standard library provides many libraries to work with strings. strings regexp unicode Creating Strings in Go To create a string you use douple quotes "" not a single quote, however you can use backtick ` for raw strings \n will be printed as \n not as a newline in raw strings, in the following snippet we use double qoutes: welco...

Go Structures

Go Structures are a user-defined data type available in Golang, which allows you to combine different values types (including structures) into one type, its similar to Classes in other object-oriented programming languages, Assume you want to keep track of the articles in a blog. You might want to track the following attributes of each article − Title Body Category Author Defining a Structure To define a structure, you must use type and struct statements. The struct statement ...

Debian Router - Dhcp Server Setup

We''ve learned  previously  how to set up unbound dns server as caching dns server for our LAN users, in this article we''ll configure dhcp server from which a unique ip address for each client assigned as a bonus automatically configure dns server settings for them.     In this debian router articles series also: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unboun...

C Recursion

Recursion in programming is the process of calling a procedure repeatedly in a self-similar way, a recursive function or routine is a function that call itself repeatedly. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an...

Using Linux Debian As Router Setup

In this articles series you will learn how to setup Linux (Debian) as a Router for home/business local networks, to spice things I''ll show how to add dns server, proxy/web caching server, and basic web traffic filter to our cute debian box.   In this series I''ll go over: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unbound dns server setup . DHCP server setup . Squid setup. ...