It's a actually a general question, but it occurred now that I'm working with Go and C#. Say we want to assign a variable from user's input in Go:
func main() {
var input float64
fmt.Scan(&input)
}
It's pretty much clear why we need a memory location to put our new value in. But why in languages like Java or C#, we are not following the same logic:
var input = Convert.ToInt32(Console.ReadLine());
// and not &input ...
Java and C# are higher level languages which abstract most of the memory management and other particular things required in lower level languages like C.
In this case, the Console.ReadLine()
function allocates memory to store the console input and copies it to the input
variable.
Since these languages have garbage collection, allocating and deallocating memory is done automatically, so the framework don't require you to explicitly pass a memory address to write to, and doesn't expect you to free the memory when you are done using it.
Edit:
See @kostix comment for a great improvement to this answer.
In Go, like C/C++, pointer variables are how types can be passed by reference.
Languages like Java and C# discourage the use of pointer variables. C# has the "ref" keyword and "boxing" for passing value types by reference.
See here for more on "ref": https://msdn.microsoft.com/en-us/library/14akc2c7.aspx See here for more on "boxing: https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx