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
+22.8k Golang : Gorilla mux routing example
+14.5k Golang : Convert(cast) int to float example
+22.5k Generate checksum for a file in Go
+43.3k Golang : Get hardware information such as disk, memory and CPU usage
+12.6k Golang : Sort and reverse sort a slice of bytes
+6.5k Unix/Linux : How to get own IP address ?
+7.8k Swift : Convert (cast) String to Double
+11.5k SSL : The certificate is not trusted because no issuer chain was provided
+12.6k Golang : Add ASCII art to command line application launching process
+9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+7k Nginx : How to block user agent ?
+19.3k Golang : Fix cannot download, $GOPATH not set error