Skip to content

Instantly share code, notes, and snippets.

@piotrpersona
Created January 5, 2022 16:03
Show Gist options
  • Save piotrpersona/2925a6aa9334f6cef34b9311f5be4a39 to your computer and use it in GitHub Desktop.
Save piotrpersona/2925a6aa9334f6cef34b9311f5be4a39 to your computer and use it in GitHub Desktop.
Custom constraint golang 1.18 generics
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