In Go, you can implement a hash table using the built-in map data structure. A hash table provides a way to store key-value pairs, where each key is unique and maps to a specific value. The Go map is implemented as a hash table, making it efficient for lookups, insertions, and deletions.
package main
import "fmt"
func main() {
// Creating a hash table using a map
hashTable := make(map[string]int)
// Adding key-value pairs to the hash table
hashTable["apple"] = 10
hashTable["banana"] = 20
hashTable["orange"] = 30
// Retrieving a value from the hash table
value := hashTable["apple"]
fmt.Println("Value for 'apple':", value)
// Checking if a key exists in the hash table
if val, exists := hashTable["banana"]; exists {
fmt.Println("Value for 'banana':", val)
} else {
fmt.Println("Key 'banana' does not exist.")
}
// Deleting a key-value pair from the hash table
delete(hashTable, "orange")
fmt.Println("Hash table after deleting 'orange':", hashTable)
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?