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
+18.9k Golang : How to get time from unix nano example
+4.7k PHP : Convert CSV to JSON with YQL example
+11.6k Golang : HTTP response JSON encoded data
+25.6k Golang : Force your program to run with root permissions
+16.3k Golang : Check if IP address is version 4 or 6
+3.8k Javascript : Access JSON data example
+10.4k Golang : How to use if, eq and print properly in html template
+20.4k Golang : Convert string slice to struct and access with reflect example
+28.7k Golang : Get and Set User-Agent examples
+8.2k Golang : Go as a script or running go with shebang/hashbang style
+18.1k Golang : Calculate entire request body length during run time