Golang : Array mapping with Interface
New comers to Golang often have slight difficulty understanding how interface works in Golang. This is a simple tutorial to demonstrate how to map arrays with interface.
package main
import (
"fmt"
)
var strArray = []string{"abc", "def", "ghi"}
var strMap = map[string]interface{}{}
var intArray = []int{1, 2, 3}
var intMap = map[int]string{}
func main() {
for i := 0; i != 3; i++ {
fmt.Println(intArray[i], "\t", strArray[i])
intMap[i] = strArray[i]
strMap[strArray[i]] = intMap
}
fmt.Println("String map : ", strMap)
fmt.Println("Integer map : ", intMap)
}
Output :
1 abc
2 def
3 ghi
String map : map[ghi:map[0:abc 1:def 2:ghi] abc:map[0:abc 1:def 2:ghi] def:map[0:abc 1:def 2:ghi]]
Integer map : map[0:abc 1:def 2:ghi]
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+27.2k Golang : Get time.Duration in year, month, week or day
+5.8k Golang : Example of custom handler for Gorilla's Path usage.
+11.3k Golang : Convert(cast) uintptr to string example
+21.7k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+7.6k Golang : Take screen shot of browser with JQuery example
+9.4k Golang : Sieve of Eratosthenes algorithm
+12k Golang : Query string with space symbol %20 in between
+7k Useful methods to access blocked websites
+20.9k Golang : Get ASCII code from a key press(cross-platform) example
+7.8k Golang : Find correlation coefficient example
+4.9k Golang : Detect face in uploaded photo like GPlus