Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created December 3, 2023 16:13
Show Gist options
  • Save Micrified/455e7e4dc1aa6bb1a28898add4e38430 to your computer and use it in GitHub Desktop.
Save Micrified/455e7e4dc1aa6bb1a28898add4e38430 to your computer and use it in GitHub Desktop.
Experimentation with handling heterogenous receivers with generics in Go
// Online Go compiler to run Golang program online
// Print "Hello World!" message
package main
import "fmt"
// Type A, normal receiver, and pointer receiver
type A struct {
s string
}
func (a A) Show() {
fmt.Println(a.s)
}
func (a *A) String() string {
return a.s
}
func show [P interface {*T; String() string}, T interface{Show()}] (x T) {
x.Show()
p := P(&x)
fmt.Println(p.String())
}
func main() {
a := A{s:"a"}
show(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment