How do I implement a hash table in Go?

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.

Example of a Hash Table in Go

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) }

Go hash table map key-value pairs data structure Go programming