> ## Content Index
> Fetch the complete content index at: https://huizhou92.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Golang 1.24: New Std-Lib weak
- URL: https://huizhou92.com/go1-24-new-std-lib-weak-2/
- Published: 2024-12-16T11:27:57.000Z
- Updated: 2026-09-08T02:29:54.000Z
- Description: Golang 1.24: New Std-Lib weak. Go 1.24 introduces a new std-lib package called weak , which allows you to create safe references to *T without preventing t。
- Author: huizhou92
- Tags: #Migrated-1788833207488, #Import 2026-09-08 02:07

`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.

### Simple Definition of `weak`

The `weak` package is defined with just a `Make` method and a `Value` method.

![weak std-lib](https://huizhou92.com/content/images/2026/09/0-vk5mpiezwm7vciuk.png)

By using `weak.Make`, a `weak.Pointer` is created, and if the original `*T` has not been GC, we can access its address through `weak.Pointer.Value`. If the object has been collected, the `Value` will return `nil`.

You can see an example implementation of `weak` here:

**Example Output:**

```shell
➜  weak git:(main) ✗ gotip version  
go version devel go1.24-18b5435 Sun Dec 15 21:41:28 2024 -0800 darwin/arm64 
➜  weak git:(main) ✗ gotip run main.go               
originalObject:addr 14000010050 
weakPtr addr:{1400000e0d0},size:8 
First GC :value:  Hello, World! 
originalObject clean at:  1734340907 
Second GC: Weak reference value is nil
```

In this example:

- We create a `string` variable `originalObject` and use `weak.Make` to create a `weak.Pointer` called `weakPtr`.
- During the first garbage collection (GC), since `originalObject` is still in use, `weakPtr.Value` returns the address of `originalObject`.
- In the second GC, since `originalObject` is no longer used, it is collected by the GC, and `weakPtr.Value` returns `nil`.

Also, `runtime.AddCleanup`, a new feature in Go 1.24, works similarly `runtime.SetFinalizer` by allowing you to execute code when an object is GC. This feature will be covered in more detail later.

#### Key Takeaways:

1. `weak.Make` creates a `weak.Pointer`, which hides the actual memory address but does not affect the GC.
2. If the real object is garbage collected, `weak.Pointer.Value` it will return `nil`. Since we don't know when the actual object will be reclaimed, always check the return value of `weak.Pointer.Value`.

---

### The Practical Use of `weak`

#### Canonicalization Maps

You might remember the `unique` feature introduced in Go 1.23, which uses a single pointer (8 bytes) to represent multiple identical strings, saving memory. The `weak` package can achieve similar functionality (In fact, in Go 1.24, `unique` has been refactored using `weak`).

#### Implementing a Fixed-Size Cache

Here’s an example of a fixed-size cache using `weak`\+ `list`.

In this example:

- We use a fixed-size list and a `Map` to store the position of each key in the list where the value is a `weak.Pointer` to a `list.Element`.
- When we add a new cache item, we first check if the list is complete. If so, the oldest item is evicted.
- When a key exists in the cache, `Map[key].Value` will return the address of the list element. If the item is evicted, `Map[key].Value` returns `nil`.

This design helps to create an efficient, fixed-size cache system. Using `weak` a lock-free queue structure allows for even more efficient data handling.

I find `weak` to be quite helpful in specific scenarios. It's easy to use, and I plan to incorporate it into my projects.

### Further Reading on `weak`:

1. [Go 1.24 Weak Documentation](https://tip.golang.org/doc/go1.24?ref=huizhou92.com#weak)
2. [Go GitHub Issue #67552](https://github.com/golang/go/issues/67552?ref=huizhou92.com)

---

[Go1.24Edit description![](https://huizhou92.com/content/images/2026/09/0-2127814b73f2d64e61a2995377ef1845d689f5e2-jpeg.jpg)](https://medium.huizhou92.com/list/96f77bdb6fb5?ref=huizhou92.com)