Skip to content

Commit

Permalink
feat: update generics (#10)
Browse files Browse the repository at this point in the history
* feat: update generics
  • Loading branch information
chyroc authored Nov 23, 2022
1 parent 3f95948 commit f8e48fd
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: ['http://sponsor.chyroc.cn/']
custom: ['https://sponsor.chyroc.cn/']
18 changes: 8 additions & 10 deletions generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ func Ptr[T any](s T) *T {
return &s
}

type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~complex64 | ~complex128
}

// NumberNoNonePtr returns the pointer of int value, if the value is zero-value, it returns nil.
func NumberNoNonePtr[T Number](s T) *T {
if s == 0 {
// NoNonePtr returns the pointer of int value, if the value is zero-value, it returns nil.
func NoNonePtr[T comparable](s T) *T {
var empty T
if s == empty {
return nil
}
return &s
}

// ValueNumber returns the value pointed to by the pointer
func ValueNumber[T Number](s *T) T {
// Value returns the value pointed to by the pointer
func Value[T any](s *T) T {
if s != nil {
return *s
}
return 0
var empty T
return empty
}

func ValueWithDefault[T any](s *T, defaultValue T) T {
Expand Down
Loading

0 comments on commit f8e48fd

Please sign in to comment.