This appendix discusses variables, pointers, and pass-by-value semantics. Many languages use pointers implicitly. In Java, for example, every object is essentially a pointer, allowing modifications of objects across method calls. Unlike Java, Go lets us decide whether to use a pointer to control memory. Grasping Go’s mechanics requires a solid understanding of pointers. To understand pointers, however, we must first understand variables. Last, we’ll explore Go’s pass-by-value mechanics.
B.1 Variables
In Go, we should declare a variable before use. Variables have a fixed type (e.g., int
), and their types can’t change after they’re declared unless we recompile our code. The compiler knows every variable’s type at compile time, leading to more reliable and efficient code.
We can declare variables with or without an initial value. If we skip the initial value, Go sets the variable to its type’s zero value. This approach reduces the risk of memory corruption or security bugs. These risks are common in C, for example, where uninitialized variables can end up with garbage values from random parts of memory, leading to reliability and security issues.
var title string #1