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
+48.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+4.8k Fix fatal error: evacuation not done in time problem
+10k Golang : How to pipe input data to executing child process?
+7.2k Golang : Routes multiplexer routing example with regular expression control
+19.3k Golang : Convert date string to variants of time.Time type examples
+6.3k Android Studio : How to detect camera, activate and capture example
+4.8k nginx : force all pages to be SSL
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+4.1k Swift : Convert (cast) Float to Int or Int32 value
+5.8k Mac/Linux/Windows : Get CPU information from command line
+11.3k Golang : Covert map/slice/array to JSON or XML format