Last active
December 23, 2016 16:03
-
-
Save hygull/d74b65b70fca4a8971014e7b44689a72 to your computer and use it in GitHub Desktop.
3 ways of using for loop with map created by hygull - https://repl.it/EigB/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
{ "Date of creation" => "07 Dec 2016 (Started after 07:35 am)" } | |
{ "Aim of program" => "To use for loop on maps in 3 ways" } | |
{ "Coded by" => "Rishikesh Agrawani" } | |
{ "Go version" => "1.7" } | |
*/ | |
package main | |
import "fmt" | |
func main() { | |
myDetailsMap := make(map[string]interface{}) | |
myDetailsMap["Name"] = "Rishikesh Agrawani" | |
myDetailsMap["Age"] = 24 | |
myDetailsMap["Salary"] = 150000.5 | |
fmt.Println(myDetailsMap) | |
//Printing key and values both...... | |
fmt.Println("\nKeys and values...") | |
for key, value := range myDetailsMap { | |
fmt.Printf("%s %v\n", key, value) | |
} | |
//Printing keys only................ | |
fmt.Println("\nKeys...") | |
for key := range myDetailsMap { | |
fmt.Printf("%s\n", key) | |
} | |
//Printing values only............... | |
fmt.Println("\nValues...") | |
for _, value := range myDetailsMap { //Using blank identifier _ | |
fmt.Println(value) | |
} | |
} | |
/* OUTPUT:- | |
map[Name:Rishikesh Agrawani Age:24 Salary:150000.5] | |
Keys and values... | |
Name Rishikesh Agrawani | |
Age 24 | |
Salary 150000.5 | |
Keys... | |
Name | |
Age | |
Salary | |
Values... | |
Rishikesh Agrawani | |
24 | |
150000.5 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment