Created
January 5, 2022 16:03
-
-
Save piotrpersona/2925a6aa9334f6cef34b9311f5be4a39 to your computer and use it in GitHub Desktop.
Custom constraint golang 1.18 generics
This file contains 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
package main | |
import ( | |
"fmt" | |
) | |
type Contstraint[I, O any] interface { | |
Process(in I) (out O) | |
} | |
func Print[I, O any](c Contstraint[I, O], in I) { | |
fmt.Println(c.Process(in)) | |
} | |
type Custom struct {} | |
func (c Custom) Process(a int) (b string) { | |
return fmt.Sprintf("%d", a) + "." | |
} | |
func main() { | |
c := Custom{} | |
Print[int, string](c, 123) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment