Skip to content

Commit

Permalink
feat: add func
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhizheng committed Sep 27, 2021
1 parent 507f2f8 commit 64acdc6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
33 changes: 33 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgo

import (
"reflect"
"sync"
)

Expand Down Expand Up @@ -103,4 +104,36 @@ func ArrayShift(s *[]interface{}) interface{} {
f := (*s)[0]
*s = (*s)[1:]
return f
}

func ArrayUnique(arr []string) []string{
size := len(arr)
result := make([]string, 0, size)
temp := map[string]struct{}{}
for i:=0; i < size; i++ {
if _,ok := temp[arr[i]]; ok != true {
temp[arr[i]] = struct{}{}
mutex.Lock()
result = append(result, arr[i])
mutex.Unlock()
}
}
return result
}

func ArraySearch(needle interface{}, hystack interface{}) (index int) {
index = -1

switch reflect.TypeOf(hystack).Kind() {
case reflect.Slice:
s := reflect.ValueOf(hystack)
sLen := s.Len()
for i := 0; i < sLen; i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
index = i
return
}
}
}
return
}
29 changes: 29 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgo

import (
"fmt"
"github.com/stretchr/testify/assert"
"log"
"testing"
Expand Down Expand Up @@ -108,3 +109,31 @@ func TestArrayShift(t *testing.T) {
assert.Equal(t, 2, len(*ary))
assert.True(t, !InArray("Winnie",*ary))
}

func TestArrayUnique(t *testing.T) {
ary := []string{
"Winnie",
"Winnie",
"Winnie",
}

uniqueAry := ArrayUnique(ary)

fmt.Println(uniqueAry)
assert.Equal(t, 1, len(uniqueAry))
assert.Equal(t, "Winnie", uniqueAry[0])
}

func TestArraySearch(t *testing.T) {
ary := []string{
"the",
"Winnie",
"Pooh",
}

find := ArraySearch("Winnie",ary)
notFind := ArraySearch("Winnie1",ary)

assert.Equal(t, 1, find)
assert.Equal(t, -1, notFind)
}
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pgo 使用golang实现一些常用的函数(包括但不限于php一些常用的内置函数)

收集一些常用的操作函数(包括但不限于php一些常用的内置函数),帮助更快的完成开发工作,并减少重复代码,
主要来源于 [https://www.php2golang.com](https://www.php2golang.com/)
主要来源于 [https://www.php2golang.com](https://www.php2golang.com/) (每个函数都增加了简单的单元测试)

## 安装
`go get -u github.com/hezhizheng/pgo`
Expand All @@ -14,6 +14,8 @@ pgo.ArrayPush()
pgo.ArrayPop()
pgo.ArrayUnshift()
pgo.ArrayShift()
pgo.ArrayUnique()
pgo.ArraySearch()

pgo.Md5("123465") // e10adc3949ba59abbe56e057f20f883e
pgo.Uniqid("") // 608a594ee0624
Expand All @@ -31,4 +33,4 @@ pgo.FileExists("path") // bool
```

## License
[MIT](./LICENSE.txt)
[MIT](./LICENSE.txt)

0 comments on commit 64acdc6

Please sign in to comment.