惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

博客园 - 我想我是青蛙

将golang程序注册为windows服务 CentOS上安装spark standalone mode(转载) 关于听歌这回事 c#读取apk 信息 golang 读取mongob数据写入sqlserver golang读取文件信息插入mongodb PetaPoco介绍 白话MongoDB(三)(转载) 白话MongoDB(二)(转载) 白话MongoDB(一) (转载) 给文章加入关键字链接 针对firefox ie6 ie7 ie8的css样式hack (转载) 好久不写日志了,现在开始,好好写了。。 sharepoint 查询calendar recurrence sharepoint之lookup字段 sharepoint获取Audiences 获取exchangeserve的calendar的item sharepoint错误处理 c#上传下载ftp(支持断点续传)
golang 通用Contains方法
我想我是青蛙 · 2013-05-23 · via 博客园 - 我想我是青蛙

今天在群里看见有人写了一个InAarry方法,主要作用是判断一个元素是否在slice中,看完后,我感觉方法应该还有扩展的空间

于是自己重新写了一个Contains方法,可以支持 slice,array,map等类型

package main

import (
    "errors"
    "fmt"
    "reflect"
)

// 判断obj是否在target中,target支持的类型arrary,slice,map
func Contain(obj interface{}, target interface{}) (bool, error) {
    targetValue := reflect.ValueOf(target)
    switch reflect.TypeOf(target).Kind() {
    case reflect.Slice, reflect.Array:
        for i := 0; i < targetValue.Len(); i++ {
            if targetValue.Index(i).Interface() == obj {
                return true, nil
            }
        }
    case reflect.Map:
        if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
            return true, nil
        }
    }

    return false, errors.New("not in array")
}

func main() {
    testMap()

    testArray()
    testSlice()
}

func testArray() {
    a := 1
    b := [3]int{1, 2, 3}

    fmt.Println(Contain(a, b))

    c := "a"
    d := [4]string{"b", "c", "d", "a"}
    fmt.Println(Contain(c, d))

    e := 1.1
    f := [4]float64{1.2, 1.3, 1.1, 1.4}
    fmt.Println(Contain(e, f))

    g := 1
    h := [4]interface{}{2, 4, 6, 1}
    fmt.Println(Contain(g, h))

    i := [4]int64{}
    fmt.Println(Contain(a, i))
}

func testSlice() {
    a := 1
    b := []int{1, 2, 3}

    fmt.Println(Contain(a, b))

    c := "a"
    d := []string{"b", "c", "d", "a"}
    fmt.Println(Contain(c, d))

    e := 1.1
    f := []float64{1.2, 1.3, 1.1, 1.4}
    fmt.Println(Contain(e, f))

    g := 1
    h := []interface{}{2, 4, 6, 1}
    fmt.Println(Contain(g, h))

    i := []int64{}
    fmt.Println(Contain(a, i))
}

func testMap() {
    var a = map[int]string{1: "1", 2: "2"}
    fmt.Println(Contain(3, a))

    var b = map[string]int{"1": 1, "2": 2}
    fmt.Println(Contain("1", b))

    var c = map[string][]int{"1": {1, 2}, "2": {2, 3}}
    fmt.Println(Contain("6", c))
}