25/04/2026 02:48am

JS2GO EP.15 Using Maps in Go and JavaScript
#Maps
#JavaScript
#Go
#programming
Maps, or key-value structures, are essential tools for efficiently storing and accessing data. Both Go and JavaScript support Maps, but each language has its own way of handling them. This article will guide you through creating, modifying, and accessing data in Maps, complete with code examples, pros and cons, and practical usage recommendations for real-world projects.
Maps in Go
In Go, Maps are a built-in type used to store key-value pairs.
Declaring and Creating a Map
package main
import "fmt"
func main() {
// Create an empty map
m := make(map[string]int)
// Add data
m["Alice"] = 25
m["Bob"] = 30
// Read data
fmt.Println("Alice:", m["Alice"]) // Alice: 25
// Delete a key
delete(m, "Bob")
// Check if a key exists
age, exists := m["Bob"]
if !exists {
fmt.Println("Bob not found") // Bob not found
}
fmt.Println(m) // map[Alice:25]
}
Pros
✨ Very fast data access and modification
✨ Key and value types are clearly defined, reducing errors
Cons
⚠️ Key and value types must be explicitly defined
⚠️ Cannot iterate in the order items were added (no order guarantee)
Maps in JavaScript
In JavaScript, you can use either Objects or Map (ES6).
Using an Object as a Map
let obj = {};
obj["Alice"] = 25;
obj["Bob"] = 30;
console.log(obj["Alice"]); // 25
delete obj["Bob"];
console.log(obj); // { Alice: 25 }
Using ES6 Map Object
let map = new Map();
// Add data
map.set("Alice", 25);
map.set("Bob", 30);
// Read data
console.log(map.get("Alice")); // 25
// Delete a key
map.delete("Bob");
// Check if a key exists
console.log(map.has("Bob")); // false
// Iterate through the Map
for (let [key, value] of map) {
console.log(key, value); // Alice 25
}
Pros
✨ Supports keys of any type, not just strings
✨ Can iterate in the order items were added (for ES6 Map)
✨ Map methods like set, get, delete, and has are easy to use
Cons
⚠️ Performance may be lower than Go when handling large datasets
⚠️ Must be careful to distinguish between Object and Map
Comparing Maps in Go and JavaScript
| Feature | Go | JavaScript |
|---|---|---|
| Key Type | Must specify type | Any type (Map) / string (Object) |
| Value Type | Must specify type | Any type |
| Iteration Order | Not guaranteed | ES6 Map: insertion order |
| Performance | High | Medium |
| Methods/Functions | built-in: make, delete | Map: set, get, has, delete |
| Use Case | Large data structures | Flexible key-value storage |
Summary and Recommendations
- Go: Maps are ideal for projects that require high performance and type safety.
- JavaScript: Maps are ideal for projects that need flexibility and keys of multiple types.
- When choosing a Map structure, consider data size, key type, and whether iteration order matters.
Next Episode
In EP.16 of the JS2GO series, we will explore Methods and Interfaces in Go vs. JavaScript to understand how to create and use functions within structs and objects effectively.
Read more
🔵 Facebook: Superdev School (Superdev)
📸 Instagram: superdevschool
🎬 TikTok: superdevschool
🌐 Website: www.superdev.school