At some point you may wish to use structs to manage state, exposing methods to let users change the state in a way that you can control. In the previo

Pointers & errors

submited by
Style Pass
2024-11-14 20:30:09

At some point you may wish to use structs to manage state, exposing methods to let users change the state in a way that you can control.

In the previous example we accessed fields directly with the field name, however in our very secure wallet we don't want to expose our inner state to the rest of the world. We want to control access via methods.

In Go if a symbol (variables, types, functions et al) starts with a lowercase symbol then it is private outside the package it's defined in.

Well this is confusing, our code looks like it should work. We add the new amount onto our balance and then the balance method should return the current state of it.

Without getting too computer-sciency, when you create a value - like a wallet, it is stored somewhere in memory. You can find out what the address of that bit of memory with &myVal.

The %p placeholder prints memory addresses in base 16 notation with leading 0xs and the escape character prints a new line. Note that we get the pointer (memory address) of something by placing an & character at the beginning of the symbol.

Leave a Comment