User Tools

Site Tools


variable_models_in_go

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
variable_models_in_go [2018/01/18 15:21]
luciano [Variable models in Go]
variable_models_in_go [2018/02/24 12:23] (current)
luciano [References]
Line 5: Line 5:
 That was surprising to me at first. My confusion was compounded by the fact that Go has no syntax or convention to distinguish values and references. Pointer variable declarations are explicit thanks to the ''​*''​ symbol. In Java, reference types are always written with an initial upper case -- like ''​String''​ -- and value types such as ''​int''​ are named with lowercase only. That was surprising to me at first. My confusion was compounded by the fact that Go has no syntax or convention to distinguish values and references. Pointer variable declarations are explicit thanks to the ''​*''​ symbol. In Java, reference types are always written with an initial upper case -- like ''​String''​ -- and value types such as ''​int''​ are named with lowercase only.
  
-In Go there are no visual clues: you have to know and remember that variables of special types like ''​map'',​ ''​chan'',​ and //slices// of any type behave as references((''​string''​ variables are also references in Go, but this never causes problems because strings are immutable in Go.)). Problems happen, for example, when you pass a slice to a function and the function changes ​its content when you were not expecting it.+In Go there are no visual clues: you have to know and remember that variables of special types like ''​map'',​ ''​chan'',​ and //slices// of any type behave as references((''​string''​ variables are also references in Go, but this never causes problems because strings are immutable in Go.)). Problems happen, for example, when you pass a slice to a function and that function changes ​the content ​of your slice when you were not expecting it.
  
  
Line 12: Line 12:
 Yes. But what are the values? Yes. But what are the values?
  
-If you have an array with 1 million elements and you pass that array to a function directly (without using a pointer), the entire array with 1 million elements will be copied to the parameter variable, which is a local variable in the function.+If you have an array with 1 million elements and you pass that array to a function directly (without using a pointer), the entire array with 1 million elements will be copied to the parameter variable, which is a local variable in the function. A variable of any array type holds all the elements of the array, just like a simple '​int'​ variable holds the bits of the integer
  
 However, if you pass a slice to a function, the elements are not copied. The value of a slice is a hidden struct describing it. That struct has three fields: However, if you pass a slice to a function, the elements are not copied. The value of a slice is a hidden struct describing it. That struct has three fields:
Line 34: Line 34:
 ===== References ===== ===== References =====
  
 +  * [[https://​golang.org/​doc/​faq#​references|Why are maps, slices, and channels references while arrays are values?]] in the official Go FAQ
   * [[https://​blog.golang.org/​go-slices-usage-and-internals|Go Slices: usage and internals]]:​ by Andrew Gerrand in the official Go Blog.    * [[https://​blog.golang.org/​go-slices-usage-and-internals|Go Slices: usage and internals]]:​ by Andrew Gerrand in the official Go Blog. 
   * [[https://​www.goinggo.net/​2013/​08/​understanding-slices-in-go-programming.html|Understanding slices]]: by Bill Kennedy, showing ''​unsafe''​ inspection of slice values.   * [[https://​www.goinggo.net/​2013/​08/​understanding-slices-in-go-programming.html|Understanding slices]]: by Bill Kennedy, showing ''​unsafe''​ inspection of slice values.
   * [[https://​www.calhoun.io/​why-are-slices-sometimes-altered-when-passed-by-value-in-go/​|Why are slices sometimes altered when passed by value in Go?]] by John Calhoun.   * [[https://​www.calhoun.io/​why-are-slices-sometimes-altered-when-passed-by-value-in-go/​|Why are slices sometimes altered when passed by value in Go?]] by John Calhoun.
 +  * [[https://​nathanleclaire.com/​blog/​2014/​08/​09/​dont-get-bitten-by-pointer-vs-non-pointer-method-receivers-in-golang/​|Don'​t Get Bitten by Pointer vs Non-Pointer Method Receivers in Golang]]
 +
  
 ===== Ping back ===== ===== Ping back =====
  
-> If you liked this page or have comments, ​talk to me on Twitter: [[https://​twitter.com/​standupdev|@StandUpDev]]+> If you like what you just read, please share it! 
 +> For comments ​or suggestionsplease ping me on Twitter: [[https://​twitter.com/​standupdev/|@standupdev]]
  
-{{tag>​golang programming_language}}+{{tag>​golang programming_language ​reference_variables}}
  
 ~~DISQUS~~ ~~DISQUS~~
variable_models_in_go.1516317702.txt.gz · Last modified: 2018/01/18 15:21 by luciano