Documentation ¶
Overview ¶
Go library for finding element in slice type or operating set including union, interaction and difference.
it not only supports the buildin types which includes []int/[]*int, []float/[]*float, []string/[]*string, but also it supports []struct/[]*struct . The latter is very important and convenient
Index ¶
- Constants
- func Contains(data interface{}, element interface{}, tag string) (isExist bool, err error)
- func GetDifference(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)
- func GetIndex(data interface{}, element interface{}, tag string) (index int, err error)
- func GetInteraction(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)
- func GetUnion(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)
Examples ¶
- Contains (Float32)
- Contains (Int)
- Contains (String)
- Contains (Uint)
- GetDifference (Float32)
- GetDifference (Int)
- GetDifference (String)
- GetDifference (Struct)
- GetDifference (Uint)
- GetIndex (Float32)
- GetIndex (Int)
- GetIndex (String)
- GetIndex (Struct)
- GetIndex (Uint)
- GetInteraction (Float32)
- GetInteraction (Int)
- GetInteraction (String)
- GetInteraction (Struct)
- GetInteraction (Uint)
- GetUnion (Float32)
- GetUnion (Int)
- GetUnion (String)
- GetUnion (Struct)
- GetUnion (Uint)
Constants ¶
const (
EPSINON float64 = 0.00001
)
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
public method,the data whether exists element
if data's type is a slice struct type, param `tag` can't be empty
Example (Float32) ¶
var ( data []float32 = []float32{1, 2, 3, 4, 5} elem float32 = 2 ) isExist, err := Contains(data, elem, "") if err != nil { fmt.Println(err.Error()) } if isExist { fmt.Println("elem in data is exist") } else { fmt.Println("elem in data is not exist") }
Output: elem in data is exist
Example (Int) ¶
var ( data []int = []int{1, 2, 3, 4, 5} elem int = 2 ) isExist, err := Contains(data, elem, "") if err != nil { fmt.Println(err.Error()) } if isExist { fmt.Println("elem in data is exist") } else { fmt.Println("elem in data is not exist") }
Output: elem in data is exist
Example (String) ¶
var ( data []string = []string{"abc", "def", "hig"} elem string = "def" ) isExist, err := Contains(data, elem, "") if err != nil { fmt.Println(err.Error()) } if isExist { fmt.Println("elem in data is exist") } else { fmt.Println("elem in data is not exist") }
Output: elem in data is exist
Example (Uint) ¶
var ( data []uint = []uint{1, 2, 3, 4, 5} elem uint = 2 ) isExist, err := Contains(data, elem, "") if err != nil { fmt.Println(err.Error()) } if isExist { fmt.Println("elem in data is exist") } else { fmt.Println("elem in data is not exist") }
Output: elem in data is exist
func GetDifference ¶
func GetDifference(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)
get difference . formula result = dataA - dataB both dataA and dataB are a slice type
Example (Float32) ¶
dataA := []float32{1, 2, 3, 4, 5} dataB := []float32{2, 4, 6, 7} temp, err := GetDifference(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("temp: ", temp) return
Output:
Example (Int) ¶
difference
dataA := []int{1, 2, 3, 4, 5} dataB := []int{2, 4, 6, 7} temp, err := GetDifference(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("temp: ", temp) return
Output:
Example (String) ¶
str1, str2, str3, str4, str5 := "1", "2", "3", "4", "5" dataA := []*string{&str1, &str2, &str3} dataB := []*string{&str2, &str3, &str4, &str5} temp, err := GetDifference(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("temp: ", temp) return
Output:
Example (Struct) ¶
type Student struct { Name string Age int } studentA := []Student{ Student{ Name: "donghai", Age: 29, }, Student{ Name: "jixaing", Age: 19, }, } studentB := []Student{ Student{ Name: "Joe", Age: 18, }, Student{ Name: "David", Age: 19, }, } if value, err := GetDifference(studentA, studentB, "Age"); err != nil { fmt.Println(err.Error()) } else { fmt.Println("studentA U studentB, result: ", value) }
Output:
Example (Uint) ¶
dataA := []uint{1, 2, 3, 4, 5} dataB := []uint{2, 4, 6, 7} temp, err := GetDifference(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("temp: ", temp) return
Output:
func GetIndex ¶
getting the element index in data, if not exists, return -1, nil
if data's type is a slice struct type, param `tag` can't be empty
Example (Float32) ¶
var ( data []float32 = []float32{1, 2, 3, 4, 5} elem float32 = 2 ) index, err := GetIndex(data, elem, "") if err != nil { fmt.Println(err.Error()) } fmt.Printf("index=%d\n", index)
Output: index=1
Example (Int) ¶
var ( data []int = []int{1, 2, 3, 4, 5} elem int = 2 ) index, err := GetIndex(data, elem, "") if err != nil { fmt.Println(err.Error()) } fmt.Printf("index=%d\n", index)
Output: index=1
Example (String) ¶
var ( data []string = []string{"abc", "def", "hig"} elem string = "def" ) index, err := GetIndex(data, elem, "") if err != nil { fmt.Println(err.Error()) } fmt.Printf("index=%d\n", index)
Output: index=1
Example (Struct) ¶
type Person struct { Name string Age int Children []string } data := []*Person{ &Person{ Name: "John", Age: 29, Children: []string{"David", "Lily", "Bruce Lee"}, }, &Person{ Name: "Joe", Age: 18, Children: []string{}, }, } elem := 18 index, err := GetIndex(data, elem, "Age") if err != nil { fmt.Println(err.Error()) } fmt.Printf("index=%d\n", index)
Output: index=1
Example (Uint) ¶
var ( data []uint = []uint{1, 2, 3, 4, 5} elem uint = 2 ) index, err := GetIndex(data, elem, "") if err != nil { fmt.Println(err.Error()) } fmt.Printf("index=%d\n", index)
Output: index=1
func GetInteraction ¶
func GetInteraction(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)
get interaction . formula result = dataA N dataB both dataA and dataB are a slice type
Example (Float32) ¶
dataA := []float32{1, 2, 3, 4, 5} dataB := []float32{2, 4, 6, 7} temp, err := GetInteraction(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [2 4]
Example (Int) ¶
interaction
dataA := []int{1, 2, 3, 4, 5} dataB := []int{2, 4, 6, 7} temp, err := GetInteraction(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [2 4]
Example (String) ¶
str1, str2, str3, str4, str5 := "1", "2", "3", "4", "5" dataA := []*string{&str1, &str2, &str3} dataB := []*string{&str2, &str3, &str4, &str5} temp, err := GetInteraction(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("temp: ", temp)
Output:
Example (Struct) ¶
type Student struct { Name string Age int } studentA := []Student{ Student{ Name: "donghai", Age: 29, }, Student{ Name: "jixaing", Age: 19, }, } studentB := []Student{ Student{ Name: "Joe", Age: 18, }, Student{ Name: "David", Age: 19, }, } if value, err := GetInteraction(studentA, studentB, "Age"); err != nil { fmt.Println(err.Error()) } else { fmt.Println("studentA U studentB, result: ", value) }
Output:
Example (Uint) ¶
dataA := []uint{1, 2, 3, 4, 5} dataB := []uint{2, 4, 6, 7} temp, err := GetInteraction(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [2 4]
func GetUnion ¶
get interaction . formula result = dataA U dataB both dataA and dataB are a slice type
Example (Float32) ¶
dataA := []float32{1, 2, 3, 4, 5} dataB := []float32{2, 4, 6, 7} temp, err := GetUnion(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [1 2 3 4 5 6 7]
Example (Int) ¶
union
dataA := []int{1, 2, 3, 4, 5} dataB := []int{2, 4, 6, 7} temp, err := GetUnion(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [1 2 3 4 5 6 7]
Example (String) ¶
str1, str2, str3, str4, str5 := "1", "2", "3", "4", "5" dataA := []*string{&str1, &str2, &str3} dataB := []*string{&str2, &str3, &str4, &str5} temp, err := GetUnion(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } value := temp.([]*string) for index := 0; index < len(value); index++ { fmt.Printf("%s ", *value[index]) }
Output: 1 2 3 4 5
Example (Struct) ¶
type Student struct { Name string Age int } studentA := []Student{ Student{ Name: "donghai", Age: 29, }, Student{ Name: "jixaing", Age: 19, }, } studentB := []Student{ Student{ Name: "Joe", Age: 18, }, Student{ Name: "David", Age: 19, }, } if value, err := GetUnion(studentA, studentB, "Age"); err != nil { fmt.Println(err.Error()) } else { fmt.Println("studentA U studentB, result:", value) }
Output: studentA U studentB, result: [{donghai 29} {jixaing 19} {Joe 18}]
Example (Uint) ¶
dataA := []uint{1, 2, 3, 4, 5} dataB := []uint{2, 4, 6, 7} temp, err := GetUnion(dataA, dataB, "") if err != nil { fmt.Println(err.Error()) } fmt.Println("result:", temp)
Output: result: [1 2 3 4 5 6 7]
Types ¶
This section is empty.