notexOPENOverview
Notex/Languages/Pointers

Pointers

Always free to read

Every variable is stored in one or more contiguous memory locations, called addresses.

04

GO / CORE LANGUAGE

Pointers

20 LESSONS · 47 MIN
  1. 04.01A Quick Pointer PrimerEvery variable is stored in one or more contiguous memory locations, called addresses.READING3 MIN
  2. 04.02Don’t Fear the PointersThe first rule of pointers is: Don’t be afraid of pointers. If you are used to Java, JavaScript, Python, or Ruby, you might find them intimidating, but pointers are actually…READING3 MIN
  3. 04.03Allocating with newThe built-in function new creates a pointer variable. It returns a pointer to an instance of the provided type or expression. If you pass a type to new, the pointer points to a…READING5 MIN
  4. 04.04Code Review: Intro to pointers, new, and shallow copiesStructs and primitive types in Go can be either pointers or values. In many other languages, primitive types are always values and classes are always pointers.PRACTICE1 MIN
  5. 04.05Practice: Intro to pointers, new, and shallow copiesThese exercises will allow you to try out some of the pointer concepts that I’ve presented. The answers to the exercises are in the exercise_solutions directory.PRACTICE1 MIN
  6. 04.06Pointers Indicate Mutable ParametersAs you’ve already seen, Go constants provide names for literal expressions that can be calculated at compile time. Go has no mechanism to declare that other kinds of values are…READING3 MIN
  7. 04.07Pointers Are a Last ResortBe careful when using pointers in Go. As discussed earlier, they make it harder to understand data flow and can create extra work for the garbage collector. Rather than…READING2 MIN
  8. 04.08Pointer Passing PerformanceIf a struct is large enough, you can improve its performance by using a pointer to the struct as either an input parameter or a return value. The time to pass a pointer into a…READING1 MIN
  9. 04.09The Zero Value Versus No ValueGo pointers are often used to indicate the difference between a variable or field that’s been assigned the zero value and one that hasn’t been assigned a value at all. If this…READING1 MIN
  10. 04.10The Difference Between Maps and SlicesAs you saw in this topic, any modifications made to a map that’s passed to a function will be reflected in the original passed-in variable. Now that you know about pointers,…READING4 MIN
  11. 04.11Slices as BuffersWhen reading data from an external resource (like a file or a network connection), many languages use code like this:READING1 MIN
  12. 04.12Code Review: Using Pointers, Slices, and MapsPrefer value parameters. Use a pointer parameter only when the function must mutate the caller’s value, the type must not be copied, or a struct is genuinely large.PRACTICE1 MIN
  13. 04.13Practice: Using Pointers, Slices, and MapsThese exercises let you see how pointers and slices interact with function calls.PRACTICE1 MIN
  14. 04.14Reducing the Garbage Collector’s WorkloadUsing buffers is just one example of how you can reduce the garbage collector’s workload. When programmers talk about “garbage,” what they mean is “data that has no more…READING8 MIN
  15. 04.15Weak PointersGarbage collection frees you from memory management, but it also limits your control. What if you want to implement a cache that automatically shrinks when memory is tight?…READING2 MIN
  16. 04.16Allocating SlicesOne of the benefits of garbage-collected languages like Go is that new versions can improve memory allocation. Your code can take advantage of these improvements with a simple…READING3 MIN
  17. 04.17Tuning the Garbage CollectorThe garbage collector doesn’t immediately reclaim memory as soon as it is no longer referenced. Doing so would seriously impact performance. Instead, it lets the garbage pile…READING4 MIN
  18. 04.18Code Review: Working with the Garbage CollectorTry to lay out your data structures sequentially in memory, so they can be loaded from RAM efficiently.PRACTICE1 MIN
  19. 04.19Practice: Working with the Garbage CollectorNow that you have learned about using pointers and memory effectively in Go, work through these exercises to reinforce your knowledge.PRACTICE1 MIN
  20. 04.20Recap: PointersThis topic peeked under the hood a bit to help you understand what pointers are, how to use them, and—​most importantly--when to use them. In this topic, you’ll see how Go…READING1 MIN
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K