Junior — Middle
Millised meetodid on olemas hash-tabeli andmestruktuuri loomise ja kasutamise jaoks?
sobes.tech AI
Vastus AI-lt
In Go (Golang), the data structure of a hash table is implemented through the built-in map type. It is an associative array that links keys to values.
Creating and using a map involves the following steps:
- Declare a variable of type
mapwith specified key and value types. - Initialize it using the
makefunction. - Add, read, and delete elements.
Example:
package main
import "fmt"
func main() {
// Create a map with string keys and int values
ages := make(map[string]int)
// Add elements
ages["Alice"] = 30
ages["Bob"] = 25
// Read an element
fmt.Println("Age of Alice:", ages["Alice"])
// Check if a key exists
age, ok := ages["Charlie"]
if ok {
fmt.Println("Age of Charlie:", age)
} else {
fmt.Println("Key Charlie not found")
}
// Delete an element
delete(ages, "Bob")
}
Thus, in Go, map is an efficient way to implement a hash table with a convenient syntax for adding, searching, and deleting operations.