i really wanna learn golang it looks fun and capable and i can read it kind of but every time i try it i’m immediately stuck on basic concepts like “what the fuck is a pointer” (this has been explained to me and i still don’t get it). i did have types explained to me as like notes on code which makes sense a bit but i’m mostly lost on basic code concepts
@kat@yarn.girlonthemoon.xyz Pointers can be a bit tricky. I know it took me also quite some time to wrap my head around them. Let my try to explain. It’s a pretty simple, yet very powerful concept with many facets to it.
A pointer is an indirection. At a lower level, when you have some chunk of memory, you can have some actual values sitting in there, ready for direct use. A pointer, on the other hand, points to some other location where to look for the values one’s actually after. Following that pointer is also called dereferencing the pointer.
I can’t come up with a good real-world example, so this poor comparison has to do. It’s a bit like you have a book (the real value that is being pointed to) and an ISBN referencing that book (the pointer). So, instead of sending you all these many pages from that book, I could give you just a small tag containing the ISBN. With that small piece of information, you’re able to locate the book. Probably a copy of that book and that’s where this analogy falls apart.
In contrast to that flawed comparision, it’s actually the other way around. Many different pointers can point to the same value. But there are many books (values) and just one ISBN (pointer).
The pointer’s target might actually be another pointer. You typically then would follow both of them. There are no limits on how long your pointer chains can become.
One important property of pointers is that they can also point into nothingness, signalling a dead end. This is typically called a null pointer. Following such a null pointer calls for big trouble, it typically crashes your program. Hence, you must never follow any null pointer.
Pointers are important for example in linked lists, trees or graphs. Let’s look at a doubly linked list. One entry could be a triple consisting of (actual value, pointer to next entry, pointer to previous entry).
_______________________
/ ________\_______________
↓ ↓ | \
+---+---+---+ +---+---+-|-+ +---+---+-|-+
| 7 | n | x | | 23| n | p | | 42| x | p |
+---+-|-+---+ +---+-|-+---+ +---+---+---+
| ↑ | ↑
\_______/ \_______/
The “x” indicates a null pointer. So, the first element of the doubly linked list with value 7 does not have any reference to a previous element. The same is true for the next element pointer in the last element with value 42.
In the middle element with value 23, both pointers to the next (labeled “n”) and previous (labeled “p”) elements are pointing to the respective elements.
You can also see that the middle element is pointed to by two pointers. By the “next” pointer in the first element and the “previous” pointer in the last element.
That’s it for now. There are heaps ;-) more things to tell about pointers. But it might help you a tiny bit.
@lyse@lyse.isobeef.org (I think of pointers as “memory location + type”, but I have done so much C and Assembler by now that the whole thing feels almost trivial to me. And I would have trouble explaining these concepts, I guess. 😅 Maybe I’ll cover this topic with our new Azubis/trainees some day …)
@movq@www.uninformativ.de Oh, right, a type would be good to have! :-D
@lyse@lyse.isobeef.org THANK YOUUUU I AM BOOKMARKING THIS FOR THE FUTURE <3