实现说明
Contains Index Equal 三个切片判断以函数 main为骨架。
使用要点
- 直接运行文件,输出会带上标签,比如
has billing:、has audit:、index of search:——输出的每一行都能指回产生它的那次调用。
常见问题
运行「Contains Index Equal 三个切片判断」会打印什么?
运行文件本身,has billing:、has audit:、index of search: 这些前缀各对应一段演示逻辑。
核心语句
features := []string{"auth", "billing", "search"}
代码示例
package main
import (
"fmt"
"slices"
)
func main() {
features := []string{"auth", "billing", "search"}
// slices.Contains: a presence check that compares with ==
fmt.Println("has billing:", slices.Contains(features, "billing"))
fmt.Println("has audit:", slices.Contains(features, "audit"))
// slices.Index: the first matching index, or -1 when nothing matches
fmt.Println("index of search:", slices.Index(features, "search"))
fmt.Println("index of audit:", slices.Index(features, "audit"))
// slices.Equal: true only when lengths and every element match
a := []int{1, 2, 3}
b := []int{1, 2, 3}
c := []int{1, 2, 4}
fmt.Println("a==b:", slices.Equal(a, b), "a==c:", slices.Equal(a, c))
// A nil slice and an empty slice both have length 0: Equal treats them as equal
var nilSlice []int
fmt.Println("nil==empty:", slices.Equal(nilSlice, []int{}))
}