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

推荐订阅源

WordPress大学
WordPress大学
T
Threat Research - Cisco Blogs
美团技术团队
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
The Register - Security
The Register - Security
Cyberwarzone
Cyberwarzone
F
Fortinet All Blogs
L
LINUX DO - 热门话题
C
Check Point Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
S
Security Affairs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
V2EX - 技术
V2EX - 技术
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Martin Fowler
Martin Fowler
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
I
InfoQ
Cisco Talos Blog
Cisco Talos Blog
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
L
Lohrmann on Cybersecurity
T
Threatpost
腾讯CDC
Security Latest
Security Latest
K
Kaspersky official blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Stack Overflow Blog
Stack Overflow Blog
Help Net Security
Help Net Security
Forbes - Security
Forbes - Security

博客园 - 川川籽

GO面试题:new 和 map 的区别 minikube dashboard ImagePullBackOff 失败问题的解决方法 hashicorp/raft模块实现的raft集群存在节点跨集群身份冲突问题 macos 13安装openvpn - 川川籽 [转发] Go pprof内存指标含义备忘录 自己搭建一个https的dns,让不同的浏览器使用不同的DNS,使用相同的域名访问到不同的主机上 记一次docker buildx build 推送到本地私有仓库出现 connection refused 的问题 Linux C 获取本机IPV4和IPV6地址列表 Mac M1 安装python3.6.x MacOS M1 安装python3.5 使用php的openssl_encrypt和python的pycrypt进行跨语言的对称加密和解密问题 golang random string 【转载】coroutine 与 goroutine 区别 python简单的time ticker 'invalid flag in #cgo LDFLAGS: -w' 问题解决 记录一次python的mysqlclient依赖库报错问题 airflow当触发具有多层subDAG的任务的时候,出现[Duplicate entry ‘xxxx’ for key dag_id]的错误的问题处理 Python3并发写文件 python hash 每次调用结果不一样
golang map 和 interface 的一些记录
川川籽 · 2022-09-27 · via 博客园 - 川川籽
  • golangmap读取是不需要判断key是否存在的,不存在的key会返回默认值。
  • 如果map的value是interface,那么interface是需要先进行类型转换的,非要求类型的转换,得到结果是nil
package main

import "fmt"

var m map[string]interface{}

func getStr(k string) string {
	v, ok := m[k].(string)
	if ok {
		return v
	} else {
		return "Null"
	}
}

func getNum(k string) int {
	v, ok := m[k].(int)
	if ok {
		return v
	} else {
		return 0
	}
}

func getFunc(k string) string {
	v, ok := m[k].(func(name string, age int) string)
	if ok {
		return v(getStr("name"), getNum("age"))
	} else {
		return "unknow"
	}

	//v, _ := m[k].(func(name string, age int) string)
	//return v("a", 12)
}

func main() {
	fmt.Println("Hello, 世界")
	m = map[string]interface{}{
		"name":      "zhangsan",
		"age":       12,
		"is_female": false,
		"hi": func(name string, age int) string {
			return fmt.Sprintf("hello, my name is %s, i'm %d year old.", name, age)
		},
	}
	fmt.Println(getStr("name"))
	fmt.Println(getStr("age"))
	fmt.Println(getStr("hi"))
	fmt.Println(getNum("age"))
	fmt.Println(getFunc("age"))
	fmt.Println(getFunc("sayhi"))
	fmt.Println(getFunc("hi"))
}