dedupe

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 2 Imported by: 9

README

What is this?

[]int{1, 2, 3, 3}, []string{"1", "2", "3", "3"}, []float64{0.1, 0.2, 0.3, 0.3} etc.
You have to implement each type by yourself when removing duplicates from existing slices.
At that time, I made something that was put together and can be used even a little.

Installation

go get -u github.com/go-utils/dedupe

Usage

import (
    "fmt"

    "github.com/go-utils/dedupe"
)

func main() {
    fmt.Println("[Case1]")
    // Basic usage
    sliceString := []string{"Go", "V", "C++", "Java", "Python", "Go", "Ruby", "C++", "Go", "V"}
    dedupe.Do(&sliceString)                               // Be sure to pass it by address
    fmt.Printf("original  slice -> %#v\n\n", sliceString) // change original slice

    fmt.Println("[Case2]")
    // Extract duplicates without changing the original slice
    // But: If it is a structure, you must cast it yourself
    sliceFloat64 := []float64{0.1, 0.1, 0.2, 0.2, 0.3, 0.3}
    dup := dedupe.NewDeduplication() // or &dedupe.Deduplication{NotChange: true}
    dup.Do(&sliceFloat64)            // Be sure to pass it by address
    if src, err := dup.Float64(); err == nil {
        fmt.Printf("extracted slice -> %#v\n", src)
    }
    fmt.Printf("original  slice -> %#v\n", sliceFloat64)
}
Result
[Case1]
original  slice -> []string{"C++", "Go", "Java", "Python", "Ruby", "V"}

[Case2]
extracted slice -> []float64{0.1, 0.2, 0.3}
original  slice -> []float64{0.1, 0.1, 0.2, 0.2, 0.3, 0.3}

Check Go Playground
Check Other patterns

Support

[]bool []float32 []float64 []int []int64 []uint []uint64 []string
[]struct []*struct []AnyType

No Support

[]func

License

MIT

Documentation

Overview

Package dedupe - Deduplication among various types of slices

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(args interface{}) error

Do simply dedupe

Types

type Deduplication

type Deduplication struct {
	SliceBool    []bool
	SliceFloat32 []float32
	SliceFloat64 []float64
	SliceInt     []int
	SliceInt64   []int64
	SliceUint    []uint
	SliceUint64  []uint64
	SliceString  []string
	SliceAny     interface{}

	CastFailed bool
	NotChange  bool
	Value      *reflect.Value
	Error      error
}

Deduplication duplicate exclusion

supports type:
  []bool, []float32, []float64, []int, []int64,
  []uint, []uint64, []string, []struct, []*struct, []AnyType
no supports type: []func

func NewDeduplication

func NewDeduplication() *Deduplication

NewDeduplication constructor NotChange field: true

func (*Deduplication) Any added in v0.2.0

func (d *Deduplication) Any() (interface{}, error)

Any returns the deduplicated slice

func (*Deduplication) Do

func (d *Deduplication) Do(args interface{}) error

Do divide processing by type

func (*Deduplication) Float32

func (d *Deduplication) Float32() ([]float32, error)

Float32 returns the deduplicated slice

func (*Deduplication) Float64

func (d *Deduplication) Float64() ([]float64, error)

Float64 returns the deduplicated slice

func (*Deduplication) Int

func (d *Deduplication) Int() ([]int, error)

Int returns the deduplicated slice

func (*Deduplication) Int64

func (d *Deduplication) Int64() ([]int64, error)

Int64 returns the deduplicated slice

func (*Deduplication) String

func (d *Deduplication) String() ([]string, error)

String returns the deduplicated slice

func (*Deduplication) Uint

func (d *Deduplication) Uint() ([]uint, error)

Uint returns the deduplicated slice

func (*Deduplication) Uint64

func (d *Deduplication) Uint64() ([]uint64, error)

Uint64 returns the deduplicated slice

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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