Golang for Rubyists. Part 3. Ampersands and asterisks. Pointers and references.
Welcome back, my fellow learners. I still on my way of learning Golang and recently stumbled upon some listing, where I’ve found asterisks and ampersands, which I couldn’t get the hang of and had to google. So I think it is a great topic for the third post in the series. Let’s first recall, where can you find asterisks (*) and ampersands (&) in Ruby? Obviously we don’t speak about logical or math functions here, it is the same in any (oh, such a broad word, ok, in most of) programming languages. So, asterisks, here is the code, which any Ruby developer understands:
So, we name this operator “splat”, it kinda opens an array. It can work the similar way in method arguments:
We usually use it to pass some additional arguments, amount or existing of which we’re not sure about. So, long story short, in Ruby we use asterisk to open an array. Or multiply numbers. Or multiply a number and a string. Or something else. And you can use two asterisks and have a different behaviour. Ok, Ruby is weird sometimes. What about ampersand? The first thing comes to our mind is double ampersand as a logical operator. Move on, what else?
Ok, arrays’ intersection. Anything else?
Yeah, our beloved blocks. I remember when I was just starting doing Ruby that syntax could blow my mind. So we use ampersand here to tell that the argument we’re expecting is a block, so it will be passed in different way than plaun arguments. Cannot recall any other usages of ampersand in Ruby from the top of my head, so let’s check out, how does Golang suggest us to use ampersands. If you have an engineering education, you’re most likely have heard about pointers from your C/C++ course. And if you’re like me - you’ve never worked with them in a real life outside of a university. So let’s refresh our memory. We assume that we have a drawer, where there is some object on each shelf. Then you look into one drawer, see that there is an orange and say to your friend, hey dude, remember “an orange”. This is a typical variable assignment. You just assigned to your friend’s memory value “an orange”. If you then will eat this delicious juicy orange and put some disgusting thing like broccoli onto that shelf - your friend will still remember “an orange”. BUT. Instead of telling your friend “please remember “an orange”, you can tell him, “dude, there is something on the second shelf of that drawer, please remember it”. He will remember where is “it” located, not the object itself. And this is a pointer assignment. Probably my real-world-explanation convoluted things just more, so let’s simply take a look at the following listing:
This code may look cumbersome but just because of comments, don’t be afraid, just go through it without hurry. Long story short, if you assign some variable to another variable, it will copy its value.
If you assign an memory address of some variable (pointer) to another variable, it will be pointing onto the same memory cell. And if you change the value of the original variable and then will try to access it from a second variable - you will get an updated value. And as you all know, in Ruby you cannot operate with pointers. And it is pass-by-value language (sarcasm). Let’s make a last small experiment to illustrate it:
Ok, that’s another story, however, if you’re curious about how Ruby actually works with objects (this is a cue, this array is not just an address in memory, it is an object) and their assignments and passing - I can recommend this article. I hope you enjoyed reading this article and get the hang of how you can use pointers in Golang and how working with variables is different from Ruby. See ya! P.S. If you missed previous posts in this series, here they are, right for you!
- Golang for Rubyists. Part 2. Go type system
- Golang for Rubyists. Part 1. Go 1.10 OSX installation and getting started
UPD. Stooop, but what about references and pass by reference, ‘cause you mentioned it in the title? Ah, Golang doesn’t have that, actually. You can either have a pointer-variable or copy the variable value to a new variable and so to a new memory cell. I can point you to this article, where you can find a deeper explanation.