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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - 我想我是青蛙

将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))
}