2

【笔记】Go查看数组或切片中是否存在指定值

 1 year ago
source link: https://loli.fj.cn/2023/04/15/Go%E6%9F%A5%E7%9C%8B%E6%95%B0%E7%BB%84%E6%88%96%E5%88%87%E7%89%87%E4%B8%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E6%8C%87%E5%AE%9A%E5%80%BC/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【笔记】Go查看数组或切片中是否存在指定值

2023-04-15

Go查看数组或切片中是否存在指定值

通过转换map找值

var list = ["a", "b"]
var l = "a"

// 将list的value作为作为map的key,将list的index作为map的value
var m = make(map[string]string)
for index, value := range list {
m[value] = index
}

// 通过转换后的map查找list的索引是否存在
if m[l] > -1 {
...
}

知乎——大川搬砖


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK