diff --git a/docs/slice.md b/docs/slice.md index 73a60f0..b5b69ab 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -20,6 +20,7 @@ import (
## Index +- [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) @@ -69,6 +70,33 @@ import ( ## Documentation +### AppendIfAbsent +If slice doesn't contain the value, append it to the slice.
+ +Signature: + +```go +func AppendIfAbsent[T comparable](slice []T, value T) []T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b"} + res1 := slice.AppendIfAbsent(strs, "a") + fmt.Println(res1) //[]string{"a", "b"} + + res2 := slice.AppendIfAbsent(strs, "cannot") + fmt.Println(res2"} +} +``` + + ### ContainCheck if the value is in the slice or not.
diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 4702a21..68c9737 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -20,6 +20,7 @@ import ( ## 目录 +- [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) @@ -69,6 +70,34 @@ import ( ## 文档 +### AppendIfAbsent +当前切片中不包含值时,将该值追加到切片中
+ +函数签名: + +```go +func AppendIfAbsent[T comparable](slice []T, value T) []T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b"} + res1 := slice.AppendIfAbsent(strs, "a") + fmt.Println(res1) //[]string{"a", "b"} + + res2 := slice.AppendIfAbsent(strs, "cannot") + fmt.Println(res2"} +} +``` + + + ### Contain判断slice是否包含value