Junior — Middle
Kuidas muutub hash-tabeli seis, kui see antakse funktsioonile parameetrina?
sobes.tech AI
Vastus AI-lt
In Go, hash tables are implemented using the map type, which is passed to functions by reference. This means that when passing a map to a function, not the table itself is copied, but a pointer to its internal data structure.
As a result, any changes made to the map inside the function (adding, removing, or modifying elements) will be visible outside the function as well, since the same object is being modified.
Example:
func addEntry(m map[string]int, key string, value int) {
m[key] = value
}
func main() {
myMap := make(map[string]int)
addEntry(myMap, "key1", 100)
fmt.Println(myMap) // Will output: map[key1:100]
}
Thus, the state of the hash table is directly changed when passing it to a function.