From 9b3acc116553d09002b75aeeb7fe84a91110e502 Mon Sep 17 00:00:00 2001 From: Dodi Triwibowo Date: Wed, 27 May 2020 21:44:36 +0700 Subject: [PATCH] Not pointer slice bug fixing --- demo/Demo.go | 11 ++++++--- utils/slice.go | 61 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/demo/Demo.go b/demo/Demo.go index 11899fe..1e7d1ef 100644 --- a/demo/Demo.go +++ b/demo/Demo.go @@ -3,6 +3,7 @@ package main import ( "fmt" "github.com/verzth/go-utils/utils" + "os" ) func main() { @@ -33,10 +34,14 @@ func main() { fmt.Printf("Array After 4: %v\n", arr) utils.Slice.AddTo(&arr, -5) // Add data with minus index, will be added to first index which is treated as 0 fmt.Printf("Array After 5: %v\n", arr) + i := utils.Slice.Exist(&arr, 9) + fmt.Println("Exist",i) + j := utils.Slice.Exist(&arr, 100) + fmt.Println("Exist",i) - utils.FileMove("/root/project/filename","/root/project/newname") // Move file from path to path - utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", "filename") // Move file from to new location with same name - utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", "filename", "newname") // Move file from to new location with new name + utils.FileMove("/root/project/filename","/root/project/newname", os.ModePerm) // Move file from path to path + utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename") // Move file from to new location with same name + utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename", "newname") // Move file from to new location with new name arrDuplicate := []int{1,2,3,2,4,5,6,7,4,4,7,4,7,7,7,15} fmt.Println(arrDuplicate) diff --git a/utils/slice.go b/utils/slice.go index 764a166..05de469 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -1,6 +1,8 @@ package utils -import "reflect" +import ( + "reflect" +) var Slice slice @@ -60,9 +62,22 @@ func (s slice) Uniquify(collections interface{}) { func (s slice) Find(collections interface{}, val interface{}) (indexes []int, state bool) { indexes = []int{}; state = false indirect := reflect.ValueOf(collections) - if indirect.IsValid() && indirect.Elem().Kind() == reflect.Slice { - for i:=0; i