Created
December 3, 2023 16:13
-
-
Save Micrified/455e7e4dc1aa6bb1a28898add4e38430 to your computer and use it in GitHub Desktop.
Experimentation with handling heterogenous receivers with generics in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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