Sitemap
The Ordinary Programmer

The Ordinary Programmer is for coders who embrace simplicity and practicality. We share real-world tips, project stories, and tech insights for developers at every level. You don’t need to be a superstar — just a programmer who loves to learn and build helpful software.

Member-only story

Golang 1.24: New Std-Lib weak

3 min readDec 16, 2024

--

Press enter or click to view image in full size
Photo by Chinmay B on Unsplash

Go 1.24 introduces a new std-lib package called weak, which allows you to create safe references to *T without preventing the garbage collector (GC) from reclaiming the memory associated with *T.

The weak package provides ways to safely reference memory weakly, meaning without preventing its reclamation by the garbage collector.

Much like OS.ROOT, weak is a feature that has existed in other programming languages for some time, including:

  • In Java, WeakReference and SoftReference are classic implementations primarily used for caching and object pools. These references allow automatic garbage collection when the JVM detects a memory shortage.
  • In Python, the weakref module allows the creation of weak references, commonly used to prevent circular reference issues or for caching.
  • In C++, std::weak_ptr was introduced alongside std::shared_ptr to solve circular dependency problems with shared pointers.
  • In Rust, Weak is the weak reference version of Rc and Arc, which helps prevent circular references and provides more flexible memory management.

--

--

The Ordinary Programmer
The Ordinary Programmer

Published in The Ordinary Programmer

The Ordinary Programmer is for coders who embrace simplicity and practicality. We share real-world tips, project stories, and tech insights for developers at every level. You don’t need to be a superstar — just a programmer who loves to learn and build helpful software.

huizhou92
huizhou92

Written by huizhou92

Golang backend engineer. Specializes in cloud, data, and security. Lifelong learner. Efficiency enthusiast.