gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/def/slice.go (about) 1 package def 2 3 func MakeSlice[E any](s []E, size int) []E { 4 if s == nil { 5 return make([]E, size) 6 } 7 return s 8 } 9 10 func SetSlice[E any](s []E, def []E) []E { 11 if s == nil { 12 return def 13 } 14 return s 15 } 16 17 func SetterSlice[E any](s []E, setter func() []E) []E { 18 if s == nil { 19 return setter() 20 } 21 return s 22 } 23 24 func MakeSliceP[E any](sp *[]E, size int) { 25 if *sp == nil { 26 *sp = make([]E, size) 27 } 28 } 29 30 func SetSliceP[E any](sp *[]E, def []E) { 31 if *sp == nil { 32 *sp = def 33 } 34 } 35 36 func SetterSliceP[E any](sp *[]E, setter func() []E) { 37 if *sp == nil { 38 *sp = setter() 39 } 40 }