Avatar ·

In Go, is a map passed by value or by reference? Why are changes inside a function visible outside?

Hello everyone! I just started learning Go and can't figure out how maps are passed to functions.\n\nI wrote this code:\n\n```go\npackage main\n\nimport "fmt"\n\nfunc modifyMap(m map[string]int) {\n m["key"] = 42\n fmt.Println("Inside function:", m)\n}\n\nfunc main() {\n myMap := map[string]int{"key": 0}\n fmt.Println("Before call:", myMap)\n modifyMap(myMap)\n fmt.Println("After call:", myMap)\n}\n```\n\nOutput:\n```\nBefore call: map[key:0]\nInside function: map[key:42]\nAfter call: map[key:42]\n```\n\nI expected the map to be passed by value (like int or string), and changes inside the function would not affect the original map. But somehow they do!\n\nAt the same time, if I try to do the same with an array:\n\n```go\nfunc modifyArray(arr [3]int) {\n arr[0]