- Published on
map[string]interface{} and type assertion in Go
- Authors
- Name
- Mike Hacker
- @ki5ibd
Tutorial for understanding the map[string]interface{}
in Go from a modern JavaScript perspective, with examples in both languages.
map[string]interface{}
in Go
Understanding In Go, a map[string]interface{}
is a way to create a map (similar to an object in JavaScript) where the keys are strings and the values can be of any type (like any
in TypeScript).
Example 1: Creating a Simple Map/Object
JavaScript:
const obj = {
name: 'John',
age: 30,
isEmployed: true,
}
console.log(obj)
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
"isEmployed": true,
}
fmt.Println(obj)
}
Example 2: Accessing Values
JavaScript:
const obj = {
name: 'John',
age: 30,
isEmployed: true,
}
console.log(obj.name) // Output: John
console.log(obj['age']) // Output: 30
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
"isEmployed": true,
}
fmt.Println(obj["name"]) // Output: John
fmt.Println(obj["age"]) // Output: 30
}
Example 3: Adding or Updating Values
JavaScript:
const obj = {
name: 'John',
age: 30,
}
obj.isEmployed = true // Adding a new key
obj.age = 31 // Updating an existing key
console.log(obj)
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
}
obj["isEmployed"] = true // Adding a new key
obj["age"] = 31 // Updating an existing key
fmt.Println(obj)
}
Example 4: Deleting Values
JavaScript:
const obj = {
name: 'John',
age: 30,
isEmployed: true,
}
delete obj.age
console.log(obj)
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
"isEmployed": true,
}
delete(obj, "age")
fmt.Println(obj)
}
Example 5: Iterating Over Values
JavaScript:
const obj = {
name: 'John',
age: 30,
isEmployed: true,
}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(`${key}: ${obj[key]}`)
}
}
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
"isEmployed": true,
}
for key, value := range obj {
fmt.Printf("%s: %v\n", key, value)
}
}
Example 6: Type Assertion
In Go, since interface{}
can hold any value, you often need to perform type assertion to work with the value.
JavaScript:
TypeScript provides some type checking and assertion features, but for this example, we'll keep it in JavaScript:
const obj = {
name: 'John',
age: 30,
isEmployed: true,
}
if (typeof obj.age === 'number') {
const age = obj.age
console.log(age) // Output: 30
}
Go:
package main
import "fmt"
func main() {
obj := map[string]interface{}{
"name": "John",
"age": 30,
"isEmployed": true,
}
if age, ok := obj["age"].(int); ok {
fmt.Println(age) // Output: 30
}
}