Sobes.tech
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:

  1. Declare a variable of type map with specified key and value types.
  2. Initialize it using the make function.
  3. 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.