1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 14:12:28 +08:00

doc: add doc for HasKey

This commit is contained in:
dudaodong
2023-07-10 10:39:55 +08:00
parent 6f48b00c88
commit 36ef5b3bd3
2 changed files with 95 additions and 1 deletions

View File

@@ -43,6 +43,7 @@ import (
- [Merge](#Merge)
- [Minus](#Minus)
- [IsDisjoint](#IsDisjoint)
- [HasKey](#HasKey)
<div STYLE="page-break-after: always;"></div>
@@ -893,7 +894,7 @@ func main() {
### <span id="IsDisjoint">IsDisjoint</span>
<p>Checks two maps are disjoint if they have no keys in common</p>
<p>Checks two maps are disjoint if they have no keys in common.</p>
<b>Signature:</b>
@@ -937,3 +938,49 @@ func main() {
// false
}
```
### <span id="HasKey">HasKey</span>
<p>Checks if map has key or not. This function is used to replace the following boilerplate code:</p>
```go
_, haskey := amap["baz"];
if haskey {
fmt.Println("map has key baz")
}
```
<b>Signature:</b>
```go
func HasKey[K comparable, V any](m map[K]V, key K) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
}
result1 := HasKey(m, "a")
result2 := HasKey(m, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```

View File

@@ -43,6 +43,7 @@ import (
- [Merge](#Merge)
- [Minus](#Minus)
- [IsDisjoint](#IsDisjoint)
- [HasKey](#HasKey)
<div STYLE="page-break-after: always;"></div>
@@ -932,3 +933,49 @@ func main() {
// false
}
```
### <span id="HasKey">HasKey</span>
<p>检查map是否包含某个key。用于代替以下样板代码:</p>
```go
_, haskey := amap["baz"];
if haskey {
fmt.Println("map has key baz")
}
```
<b>函数签名:</b>
```go
func HasKey[K comparable, V any](m map[K]V, key K) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
}
result1 := HasKey(m, "a")
result2 := HasKey(m, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```