slicelement

package module
v0.0.0-...-38d1b8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2017 License: Apache-2.0 Imports: 4 Imported by: 1

README

package slicelement

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

Installation

Standard go get:

    $  go get -v -u github.com/1046102779/slicelement

Index

//  find the element whether exists in data, if exist, return true, nil 
func Contains(data interface{}, elem interface{}, tag string) (bool, error)

// get the element index in data, if not exist, return -1, nil. 
func GetIndex(data interface{}, elem interface{}, tag string) (int, error)

// set difference, formula:  dataC =  dataA - dataB
// Param tag: unique key.  if not , it may be covered.
// eg. type User struct { UserId int, Name string, Tel string, Sex int16}
// var ( usersA, usersB []*User )
func GetDifference(dataA interface{}, dataB interface{}, tag string) (interface{}, error)

// set interaction, formula: dataC = dataA ∩ dataB , it also supports slice struct
// Param tag: unique key.  if not , it may be covered.
// eg. type User struct { UserId int, Name string, Tel string, Sex int16}
// var ( usersA, usersB []*User )
func GetInteraction(dataA interface{}, dataB interface{}, tagName string) (interface{}, error) 

// set union, formula: dataC = dataA U dataB
// Param tag: unique key.  if not , it may be covered.
// eg. type User struct { UserId int, Name string, Tel string, Sex int16}
// var ( usersA, usersB []*User )
func GetUnion(dataA interface{}, dataB interface{}, tagName string) ( interface{}, error)

desc: if the data's type is not []*struct/[]struct, the tag value is empty

Usage & Example

For usage and examples see the Godoc

example 1: []struct GetIndex
data := []*Person{
    &Person{
        Name:     "John",
        Age:      29,
        Children: []string{"David", "Lily", "Bruce Lee"},
    },
    &Person{
        Name:     "Joe",
        Age:      18,
        Children: []string{},
    },
}
elem := 18
index, err := slicelement.GetIndex(data, elem, "Age")
if err != nil {
    fmt.Println(err.Error())
}
fmt.Println("index=", index)
// output: index=1
example 2: []struct GetUnion
type Student struct {
    Name string
    Age  int
}
studentA := []Student{             studentB := []Student{
    Student{                            Student{
        Name: "donghai",                    Name: "Joe",
        Age:  29,                           Age:  18,
    },                                  },
    Student{                            Student{
        Name: "jixaing",                    Name: "David",
        Age:  19,                           Age:  19,
    },                                  },
}                                   }

if studentC, err := slicelement.GetUnion(studentA, studentB, "Age"); err != nil {
    fmt.Println(err.Error())
} else {
    fmt.Println("result: ", studentC)
}
// result:  [{donghai 29} {jixaing 19} {Joe 18}]  
// {"David", 19} is covered by {"jixiang", 19}

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

Examples

Constants

View Source
const (
	EPSINON float64 = 0.00001
)

Variables

This section is empty.

Functions

func Contains

func Contains(data interface{}, element interface{}, tag string) (isExist bool, err error)
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

func GetIndex(data interface{}, element interface{}, tag string) (index int, err error)

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

func GetUnion(dataA interface{}, dataB interface{}, tagName string) (result interface{}, err error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL