# Polymorphic struct fields

**URL:** <https://forum.golangbridge.org/t/polymorphic-struct-fields/35985>\
**Category:** Getting Help\
**Created:** [August 17, 2024, 3:22am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985 "2024-08-17T03:22:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwantum](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/q/a6a055/32.png) [@qwantum](https://forum.golangbridge.org/u/qwantum)\
**Post date:** [August 17, 2024, 3:22am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/1 "2024-08-17T03:22:00Z")

</div>

(new to Go)

I’d like to create a struct field, which could hold pointers to two different types of structs. Example:

```auto
type Letter struct {
    ...
    Previous *Character // this is invalid, I don't have this type, it's just an example
}

type Punct struct {
    ...
    Previous *Character // this is invalid, I don't have this type, it's just an example
}

l1 := Letter{}
l2 := Letter{ Previous: &l1, }
p1 := Punct{ Previous: &l2, }
p2 := Punct{ Previous: &p1, }

```

The idea (simplified) is that a string consists of characters, which can either be letters or punctuation signs (no I cannot use rune type here, because each struct contains some other useful information I need to associate with each character). Each character has a reference to the previous character – thus the `Previous` field. As you can see, in each struct `Previous` should be assigned a pointer to either a struct of a different type or a struct of the same type.

But I’m not sure how to go about polymorphism here and which type should the `Previous` field be declared as. Interfaces don’t seem to make sense here, because they deal with methods, not field types. How would you go about this?

---

<div class="post-metadata">

**Author:** ![peakedshout](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/peakedshout/32/15444_2.png) [@peakedshout](https://forum.golangbridge.org/u/peakedshout)\
**Post date:** [August 17, 2024, 5:45am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/2 "2024-08-17T05:45:01Z")

</div>

```auto
type Character sturct {
    Pre *Character
    Letter * Letter
    Punct * Punct
}
l1:=Character{Letter:&Letter{}}
l2:=Character{Pre:&l1,Letter:&Letter{}}
p1:=Character{Pre:&l2,Punct:&Punct{}}
p2:=Character{Pre:&p1,Punct:&Punct{}}

```

What should you focus on as a single element? As for whether this cell is a letter or a symbol, this is a property of this unit.

---

<div class="post-metadata">

**Author:** ![peakedshout](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/peakedshout/32/15444_2.png) [@peakedshout](https://forum.golangbridge.org/u/peakedshout)\
**Post date:** [August 17, 2024, 5:46am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/3 "2024-08-17T05:46:56Z")

</div>

While it is possible to achieve the desired result through the interface, it may be necessary to call the assertion type.

---

<div class="post-metadata">

**Author:** ![qwantum](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/q/a6a055/32.png) [@qwantum](https://forum.golangbridge.org/u/qwantum)\
**Post date:** [August 17, 2024, 5:48am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/4 "2024-08-17T05:48:09Z")

</div>

Oh, thanks, this looks very obvious. Only problem is, it doesn’t quite scale and would require an additional field in the `Character` struct for each new struct I might decide to add. Wonder if there are other ways to achieve the same goal (I was hoping maybe my understanding of interfaces is simply lacking and there’s a solution involving interfaces).

---

<div class="post-metadata">

**Author:** ![qwantum](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/q/a6a055/32.png) [@qwantum](https://forum.golangbridge.org/u/qwantum)\
**Post date:** [August 17, 2024, 5:49am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/5 "2024-08-17T05:49:09Z")

</div>

Would you be able to briefly outline how would one do it with interfaces?

---

<div class="post-metadata">

**Author:** ![peakedshout](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/peakedshout/32/15444_2.png) [@peakedshout](https://forum.golangbridge.org/u/peakedshout)\
**Post date:** [August 17, 2024, 5:53am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/6 "2024-08-17T05:53:27Z")

</div>

```auto
type Letter struct {
    Previous any
}

type Punct struct {
    Previous any
}

l1 := Letter{}
l2 := Letter{ Previous: &l1, }
p1 := Punct{ Previous: &l2, }
p2 := Punct{ Previous: &p1, }
// Like this, but to call, it has to be like this:
l,ok:=p1.Previous.(*Letter)
if ok{
}

```

This is simply a simple use of any to represent the interface, and will actually add some specific methods to avoid other structure type implementations and cause code confusion.

---

<div class="post-metadata">

**Author:** ![qwantum](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/q/a6a055/32.png) [@qwantum](https://forum.golangbridge.org/u/qwantum)\
**Post date:** [August 17, 2024, 6:01am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/7 "2024-08-17T06:01:11Z")

</div>

Right. I thought about using `any`, although that seemed a little broad. I don’t need to be particularly strict with my types, but, I suppose, it also wouldn’t hurt if I could understand how to achieve Go-style equivalent of polymorphism & inheritance in OOP, something like this (pseudo OOP-code):

```auto
class Character { ... }
class Letter extends Character {
    previous Character 
}
class Punct extends Character {
    previous Character
}
l1 = new Letter();
l2 = new Letter();
p1 = new Punct();
p2 = new Punct();
l2.previous = l1; 
p1.previous = l2;
p2.previous = p1;

```

This code restricts the `previous` field to instances of classes, that are descendants of `Character`.

---

<div class="post-metadata">

**Author:** ![peakedshout](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/peakedshout/32/15444_2.png) [@peakedshout](https://forum.golangbridge.org/u/peakedshout)\
**Post date:** [August 17, 2024, 6:11am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/8 "2024-08-17T06:11:51Z")

</div>

```auto
type Character interface {
    Pre()Character
    ...
}
type Letter struct {
    Previous Character
}
func(l *Letter)Pre()Character{
    returen l.Previous
}

type Punct struct {
    Previous Character
}
func(p *Punct)Pre()Character{
    returen p.Previous
}

```

I don’t think you understand what an interface is.  
Any is just a simple interface type, I’m just using any as an example. Actually, what you should do should be something like this.

---

<div class="post-metadata">

**Author:** ![lemarkar](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/lemarkar/32/12652_2.png) [@lemarkar](https://forum.golangbridge.org/u/lemarkar)\
**Post date:** [August 17, 2024, 7:12pm UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/9 "2024-08-17T19:12:10Z")

</div>

You can use generics to achieve the same thing without `any`.

---

<div class="post-metadata">

**Author:** ![peakedshout](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/peakedshout/32/15444_2.png) [@peakedshout](https://forum.golangbridge.org/u/peakedshout)\
**Post date:** [August 18, 2024, 2:29am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/10 "2024-08-18T02:29:58Z")

</div>

Generic? This is a good approach, but you also need to define an interface as a constraint for this scenario. If it’s just an example, I probably won’t use generics, but try to generalize and abstract into interfaces, which in my opinion is more extensible. Of course, specific issues have to be analyzed on a case-by-case basis, and this is just my personal opinion.

---

<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:** [November 16, 2024, 2:30am UTC](https://forum.golangbridge.org/t/polymorphic-struct-fields/35985/11 "2024-11-16T02:30:20Z")

</div>

Closed on November 16, 2024, 2:30am UTC

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