title: How to define type constraint on pointer receiver inGolang
body: <p>I may have missed something in the golang generics specifications and I have a weird situation that I cannot seem to find answer for.</p>
  <p>The gist is that I want to define a method that receives parameters whose pointer receiver implements a particular interface. I need it to be pointer receiver as to mutate the struct implementing the interface.</p>
  <p>Here is a simplified example of the problematic situation.  <a href="https://go.dev/play/p/Lw256Mh1pGH" rel="nofollow noreferrer">https://go.dev/play/p/Lw256Mh1pGH</a></p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import &quot;fmt&quot;
  
  type Doer interface {
      Do()
  }
  
  type Thing struct {
      used int
  }
  
  var _ Doer = &amp;Thing{}
  
  func (t *Thing) Do() {
      t.used++
      fmt.Println(&quot;Doing my thing.&quot;)
  }
  
  func Append[T Doer](list []T, constructor func() *T) []T {
      v := constructor()
      v.Do()
      return append(list, *v)
  }
  
  func main() {
      constructor := func() *Thing { return &amp;Thing{} }
      s := make([]Thing, 0)
      s = Append(s, constructor)
      fmt.Println(s)
  }
  </code></pre>
  <p>So you see there are two errors. Even if I accept the constructor to return a Thing and not a pointer possibly resulting in many allocations I still cannot make a call to <code>Do()</code>. Below is the more verbose error.</p>
  <pre><code>Thing does not implement Doer: wrong method signature
      got  func (*Thing).Do()
      want func (Doer).Do()
  </code></pre>
  <p>I wonder how to specify the type constraint such that it validates the pointer receiver. I guess I am missing some syntax?</p>
  <p>For the time being I can workaround by providing a wrapper function to call the <code>Do()</code> function. <a href="https://play.golang.com/p/AS6YN_IsgHi" rel="nofollow noreferrer">https://play.golang.com/p/AS6YN_IsgHi</a></p>
  <pre class="lang-golang prettyprint-override"><code>func Append[T any](list []T, constructor func() *T, do func(*T)) []T {
      v := constructor()
      do(v)
      return append(list, *v)
  }
  
  func main() {
      constructor := func() *Thing { return &amp;Thing{} }
      s := make([]Thing, 0)
  
      s = Append(s, constructor, func(t *Thing) { t.Do() })
  
      fmt.Println(s)
  }
  </code></pre>
  <p>Apologies for the possibly silly question. I just cannot figure out how to tell golang to check types constraints on the pointer receiver. I tried things like <code>[T *Doer]</code>, <code>[&amp;T Doer]</code> and could not get them to work.</p>