Documentation ¶
Overview ¶
Package slices contains some slice related helper functions.
Example ¶
package main import ( "fmt" "github.com/Akagi201/utilgo/slices" ) type Tag struct { Name string } type Post struct { Title string Tags []*Tag } func main() { post := &Post{ Title: "GOLANG", Tags: []*Tag{ {"Go"}, {"Golang"}, {"Gopher"}, }, } s, _ := slices.ToStrings(post.Tags, "Name") fmt.Println(s) }
Output: [Go Golang Gopher]
Index ¶
- Variables
- func IndexOf(val interface{}, slice interface{}) int
- func MaxFloat(slice []float64) float64
- func MaxInt(slice []int) int
- func MinFloat(slice []float64) float64
- func MinInt(slice []int) int
- func SumFloat(slice []float64) float64
- func SumInt(slice []int) int
- func ToBools(slice interface{}, fieldName string) (s []bool, err error)
- func ToBoolsUnsafe(slice interface{}, fieldName string) []bool
- func ToFloats(slice interface{}, fieldName string) (s []float64, err error)
- func ToFloatsUnsafe(slice interface{}, fieldName string) []float64
- func ToInts(slice interface{}, fieldName string) (s []int, err error)
- func ToIntsUnsafe(slice interface{}, fieldName string) []int
- func ToStrings(slice interface{}, fieldName string) (s []string, err error)
- func ToStringsUnsafe(slice interface{}, fieldName string) []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotSlice happens when the value passed is not a slice. ErrNotSlice = errors.New("not slice") // ErrNotString happens when the value of the field is not a string. ErrNotString = errors.New("not string") // ErrNotInt happens when the value of the field is not an int. ErrNotInt = errors.New("not int") // ErrNotFloat happens when the value of the field is not a float64. ErrNotFloat = errors.New("not float64") // ErrNotBool happens when the value of the field is not a bool. ErrNotBool = errors.New("not bool") )
Functions ¶
func IndexOf ¶
func IndexOf(val interface{}, slice interface{}) int
IndexOf returns the first index at which a given element can be found in the slice, or -1 if it is not present. The second argument must be a slice or array.
Example ¶
package main import ( "fmt" "github.com/Akagi201/utilgo/slices" ) func main() { s := []string{"a1", "a2", "aa"} { v := "aa" i := slices.IndexOf(v, s) fmt.Println(i) } { v := "bb" i := slices.IndexOf(v, s) fmt.Println(i) } }
Output: 2 -1
func ToBoolsUnsafe ¶
ToBoolsUnsafe maps a field to a slice of bool but not returns an error. If an error still happens, s will be nil.
func ToFloatsUnsafe ¶
ToFloatsUnsafe maps a field to a slice of float but not returns an error. If an error still happens, s will be nil.
func ToIntsUnsafe ¶
ToIntsUnsafe maps a field to a slice of int but not returns an error. If an error still happens, s will be nil.
func ToStringsUnsafe ¶
ToStringsUnsafe maps a field to a slice of string but not returns an error. If an error still happens, s will be nil.
Types ¶
This section is empty.