Skip to content

Instantly share code, notes, and snippets.

@takekazuomi
Created May 5, 2022 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takekazuomi/60e71f48441fc0ab155be2b7c68064c2 to your computer and use it in GitHub Desktop.
Save takekazuomi/60e71f48441fc0ab155be2b7c68064c2 to your computer and use it in GitHub Desktop.
constraint struct fields
package main
import (
"fmt"
"time"
)
type common struct {
ID string
Created time.Time
}
type entity[T any] struct {
common
data T
}
type userEntity struct {
common
FirstName string
LastName string
}
type otherEntity struct {
common
FirstName string
LastName string
}
type Vector[T constraint] []T
type constraint interface {
common | userEntity
}
func (v *Vector[T]) PrintID() {
e := (*v)[0]
fmt.Printf("hello %v", e.ID)
}
func main() {
// var v = Vector[otherEntity]{}
var v = Vector[userEntity]{}
v.PrintID()
}
@takekazuomi
Copy link
Author

maybe here

golang/go#48522

@takekazuomi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment