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
+31.3k Golang : How to check if slice or array is empty?
+7.9k Golang : Convert file content to Hex
+3.6k Javascript : Shuffle or randomize array example
+11.3k Golang : Chunk split or divide a string into smaller chunk example
+4.8k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+7.1k Golang : How to check if a string with spaces in between is numeric?
+3.1k Golang : Calculate a pip value and distance to target profit example
+14.4k Golang : Parse date string and convert to dd-mm-yyyy format
+5.7k Golang : Command line ticker to show work in progress
+2.9k Java : Generate multiplication table example
+16.7k Golang : How to make function callback or pass value from function as parameter?
+4.2k Golang : How to write backslash in string?