# How to handle flow-style interface of embedded type

**URL:** <https://forum.golangbridge.org/t/how-to-handle-flow-style-interface-of-embedded-type/38696>\
**Category:** Getting Help\
**Created:** [May 7, 2025, 10:13am UTC](https://forum.golangbridge.org/t/how-to-handle-flow-style-interface-of-embedded-type/38696 "2025-05-07T10:13:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmeier](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/pmeier/32/15768_2.png) [@pmeier](https://forum.golangbridge.org/u/pmeier)\
**Post date:** [May 7, 2025, 10:13am UTC](https://forum.golangbridge.org/t/how-to-handle-flow-style-interface-of-embedded-type/38696/1 "2025-05-07T10:13:09Z")

</div>

Assume I have a third-party type that implements a flow-style interface, i.e. calling a function on a pointer function update the state and returns the pointer back:

```go
type ThirdPartyType struct {}

func NewThirdPartyType() *ThirdPartyType {
    return &ThirdPartyType{}
}

func (tpt *ThirdPartyType) Foo(foo string) *ThirdPartyType {
    // ...
    return tpt
}

```

Using such a type is succinct:

```go
tpt := NewThirdPartyType().Foo("foo")

```

I want to wrap this `ThirdPartyType` and add a few new methods. For this I embed it into `MyType` like

```go
type MyType struct {
    *ThirdPartyType
}

func NewMyType() *MyType {
    return &MyType{}
}

func (mt *MyType) Bar(bar string) *MyType {
    // ...
    return mt
}

```

While for my custom methods I can retain the flow style, I cannot for the ones “inherited” by `ThirdPartyType` as they return `*ThirdPartyType` and thus cannot be used in the chain:

```auto
var mt *MyType = NewMyType().Bar("bar")
mt.Foo("foo")

```

This is rather unfortunate as it defeats the succinctness I had before. The only solution I found so far is to re-implement the methods of `ThirdPartyType` on `MyType` like:

```auto
func (mt *MyType) Foo(foo string) *MyType {
    mt.Foo(foo)
    return mt
}

```

Now I can in fact do

```go
mt := NewMyType().Bar("bar").Foo("foo")

```

However, this is quite a bit of boilerplate. Plus, the number of methods I’d need to re-implement is around 50, so I’d rather not maintain this by hand.

The best solution I came up with is to use code generation coupled with `reflect` to handle this automatically. I just want to check if I’m missing a simpler solution before I go down this route.

---

<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:** [May 7, 2025, 3:08pm UTC](https://forum.golangbridge.org/t/how-to-handle-flow-style-interface-of-embedded-type/38696/2 "2025-05-07T15:08:17Z")

</div>

Hello there. Sadly, as far as I know there is no solution for this question. Digging deeper I guess it’s also more about design of the implementation. In particular, I don’t like methods chaining and try to avoid it in go as much as possible and favor the `functional options` pattern. If you wanna stick with the chaining, I’d advise to checkout `gorm` source code. I’ve never seen anything better with chaining, than this package

---

<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 10, 2025, 4:12pm UTC](https://forum.golangbridge.org/t/how-to-handle-flow-style-interface-of-embedded-type/38696/4 "2025-08-10T16:12:08Z")

</div>

Closed on August 10, 2025, 4:12pm UTC

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