# Weak Pointers with Embedded Interface Types - Type Casting Workaround

**URL:** <https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901>\
**Category:** Technical Discussion\
**Created:** [May 11, 2026, 6:55am UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901 "2026-05-11T06:55:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![filcuc](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/filcuc/32/17607_2.png) [@filcuc](https://forum.golangbridge.org/u/filcuc)\
**Post date:** [May 11, 2026, 6:55am UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/1 "2026-05-11T06:55:33Z")

</div>

I’m using Go 1.22+ weak pointers to implement a read-only iterator that holds a weak reference to a file system. The issue is that my file system is stored as an interface field, and that interface embeds another interface.

The Problem:

I have:

```go
type FileStorageGargoyle struct {
    mu sync.RWMutex
    fs gargoyle.FileSystem // interface type
}

type FileStorageGargoyleReadFileIterator struct {
    fs weak.Pointer[gargoyle.Snapshot] // want weak ptr to embedded interface
}

func (s *FileStorageGargoyle) ReadFile(fileId string (io.ReadSeekCloser, error) {
    s.mu.RLock()
    defer s.mu.RUnlock()
    
    // This doesn't work - type mismatch:
    // fs: weak.Make(&s.fs), // s.fs is FileSystem, not Snapshot
}

```

Where `gargoyle.FileSystem` embeds `gargoyle.Snapshot`:

```go
type FileSystem interface {
    Snapshot // embedded
    CreateFile(id string) error
    // ... other methods
}

```

**What I Tried:**

1. Direct assignment: weak.Make(&s.fs) — compilation error, type mismatch

2. Type assertion: weak.Make(&(s.fs.(gargoyle.Snapshot))) — creates a local variable, weak pointer becomes dangling

3. Variable copy:

```go
var fs gargoyle.Snapshot = s.fs
weak.Make(&fs) // Also dangling - fs is on the stack

```

The Workaround (Works, but feels awkward):

```go
import "unsafe"

fs: weak.Make((*gargoyle.Snapshot)(unsafe.Pointer(&s.fs))),

```

This works because FileSystem embeds Snapshot, so the memory layout is compatible. The unsafe.Pointer cast lets me point directly to the s.fs field in the struct, which persists for the object’s lifetime.

My Questions:

1. Is this the intended way to handle weak pointers to embedded interface fields?
2. Are there safer alternatives that don’t involve unsafe?
3. Should the weak package documentation cover this use case?
4. Is there a Go idiom I’m missing here?
5. I’d appreciate any feedback or suggestions!

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [May 11, 2026, 8:53am UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/2 "2026-05-11T08:53:27Z")

</div>

An embedded struct field is just like a normal struct field with some added syntax sugar. `gargoyle.FileSystem` has a field named “Snapshot” which is of type `gargoyle.Snapshot`. If you want a reference to this field, you should make a weak pointer to that field: `weak.Make(&s.fs.Snapshot)`

---

<div class="post-metadata">

**Author:** ![filcuc](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/filcuc/32/17607_2.png) [@filcuc](https://forum.golangbridge.org/u/filcuc)\
**Post date:** [May 11, 2026, 9:52pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/3 "2026-05-11T21:52:06Z")

</div>

Doesn’t seems to work. Are you sure about that?

---

<div class="post-metadata">

**Author:** ![filcuc](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/filcuc/32/17607_2.png) [@filcuc](https://forum.golangbridge.org/u/filcuc)\
**Post date:** [May 15, 2026, 4:19pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/4 "2026-05-15T16:19:30Z")

</div>

i found a solution with generics

```go
package main

import (
	"weak"
)

type Base interface {
	BaseFunc()
}

type Derived interface {
	Base
	DerivedFunc()
}

type Foo struct {
}

func (f Foo) BaseFunc() {
	println("BaseFunc")
}

func (f Foo) DerivedFunc() {
	println("DerivedFunc")
}

type BadCase struct {
	ptr weak.Pointer[Base]
}

type GoodCase[T Base] struct {
	ptr weak.Pointer[T]
}

func main() {
	b := Foo{}

	b.BaseFunc()

	_ = BadCase{ptr: weak.Make(&b)}
	_ = GoodCase[Foo]{ptr: weak.Make(&b)}
}

```

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [May 16, 2026, 12:45pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/6 "2026-05-16T12:45:10Z")

</div>

Oh I just realized you were working with embedded interfaces and not embedded structs. So this should even be less of a problem, since interfaces are just contracts. Your pointer is always to a struct and you can cast it to any interface it implements at runtime or compile time.

You have found one way using generics, but you should not even need them, because weak.Pointer is already generic. You can just cast your interface to the type you need, or just provide an explicit type parameter when instanciating your weak pointer like `var p weak.Pointer[Base]`

---

<div class="post-metadata">

**Author:** ![filcuc](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/filcuc/32/17607_2.png) [@filcuc](https://forum.golangbridge.org/u/filcuc)\
**Post date:** [May 16, 2026, 3:56pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/7 "2026-05-16T15:56:28Z")

</div>

I think what you’re describing are my first attempts..and all failed.

The point is that i’m implementing an iterator that should work on all types that implement “Base” interface. The point is that being an iterator should not “own” the datastructure its iterating (this is a requirement on my implementation). The point is that the expression weak.Make(&k) create a weak.Pointer[K] where K is the type of &k. The point is there’s not implicit conversion from weak.Pointer[K] to another weak.Pointer[T] (even if K embeds T).  
I think using generics solves my problem.. with that said thank you for you help!

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [May 18, 2026, 1:13pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/8 "2026-05-18T13:13:11Z")

</div>

You are right. I didn’t understand at first that your want a generic iterator that accepts any interface value which might satisfy your Base interface. Since an interface value is effectively a typed strong pointer, you are passing a pointer to a strong pointer to weak.Make, effectively creating a weak pointer to a strong typed pointer (an interface value).

In this case generics are the only way, since you have to accept different types (could be a struct or an interface value) anything which can fulfill the interface Base. So your iterator will be a generic type accepting anything which extends Base, which will allow you to call the method after unwrapping the value from the weak Pointer.

By the way: I think your idea with using unsafe.Pointer is actually undefined behavior and may break. Since an interface value is actually a combination of a pointer to the underlying struct combined with an itab (a table to jump to the function calls) just casting it to a different type will expect a different itab and they may be completely different.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 16, 2026, 2:12pm UTC](https://forum.golangbridge.org/t/weak-pointers-with-embedded-interface-types-type-casting-workaround/41901/10 "2026-08-16T14:12:19Z")

</div>

Closed on August 16, 2026, 2:12pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
