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
+11.5k Golang : Clean formatting/indenting or pretty print JSON result
+18k Golang : Iterating Elements Over A List
+6.8k Golang : Gorrila mux.Vars() function example
+38.9k Golang : Remove dashes(or any character) from string
+5.8k Golang : Process non-XML/JSON formatted ASCII text file example
+8.5k Golang : How to capture return values from goroutines?
+6.7k Golang : A simple forex opportunities scanner
+13.8k Golang : How to pass map to html template and access the map's elements
+13.3k Golang : Tutorial on loading GOB and PEM files
+17k Golang : Clone with pointer and modify value
+5.7k PageSpeed : Clear or flush cache on web server
+25.9k Golang : Calculate future date with time.Add() function