使用空接口或空结构作为映射值有何区别? [英] Any difference in using an empty interface or an empty struct as a map's value?
本文介绍了使用空接口或空结构作为映射值有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这种构造来模拟一个集合
I am using this construct to simulate a set
type MyType uint8
map[MyType]interface{}
然后我添加所有密钥并将其映射到nil
.
I then add in all my keys and map them to nil
.
我了解到也可以使用
map[MyType]struct{}
使用空结构与interface{}
相比有什么好处.
Any benefits of using the empty struct versus interface{}
.
推荐答案
内存使用情况.例如,键入struct{}
,interface{}
和bool
,
Memory usage. For example, types struct{}
, interface{}
, and bool
,
package main
import (
"fmt"
"unsafe"
)
func main() {
var s struct{}
fmt.Println(unsafe.Sizeof(s))
var i interface{}
fmt.Println(unsafe.Sizeof(i))
var b bool
fmt.Println(unsafe.Sizeof(b))
}
输出(用于32位体系结构的字节):
Output (bytes for 32-bit architecture):
0
8
1
输出(用于64位体系结构的字节):
Output (bytes for 64-bit architecture):
0
16
1
参考:
这篇关于使用空接口或空结构作为映射值有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文