From fc3e94df58b095fc78489053add94513d65f4be0 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 27 Jul 2022 15:44:54 +0800 Subject: [PATCH] doc: add document for AppendIfAbsent function --- docs/slice.md | 28 ++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) 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"} +} +``` + + ### Contain

Check 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