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
+4.6k Golang : Get S3 or CloudFront object or file information
+11.8k Golang : Convert IPv4 address to packed 32-bit binary format
+4.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+13.5k Golang : Save(pipe) HTTP response into a file
+10.7k Golang : md5 hash of a string
+20.9k Golang : Read directory content with filepath.Walk()
+8.2k Golang : Extract or copy items from map based on value
+39.2k Golang : How to count duplicate items in slice/array?
+17.6k Golang : Populate dropdown with html/template example
+5.9k Golang : Normalize email to prevent multiple signups example
+8.2k Golang : Scramble and unscramble text message by randomly replacing words
+20.4k Golang : Convert seconds to minutes and remainder seconds