空结构体的优点
- 因为空结构体不占据内存空间,因此被广泛作为各种场景下的占位符使用
Go 语言中,可以使用 unsafe.Sizeof 计算出一个数据类型实例需要占用的字节数
import (
"fmt"
"unsafe"
)
func main() {
//0
fmt.Println(unsafe.Sizeof(struct{}{}))
}
实现集合Set
- Go 语言标准库没有提供 Set 的实现,通常使用 map 来代替。事实上,对于集合来说,只需要 map 的键,而不需要值。即使是将值设置为 bool 类型,也会多占据 1 个字节,那假设 map 中有一百万条数据,就会浪费 1MB 的空间
- 因此呢,将 map 作为集合(Set)使用时,可以将值类型定义为空结构体,仅作为占位符使用即可
type Set map[string]struct{}
func (s Set) Has(key string) bool {
_, ok := s[key]
return ok
}
func (s Set) Add(key string) {
s[key] = struct{}{}
}
func (s Set) Delete(key string) {
delete(s, key)
}
func main() {
s := make(Set)
s.Add("Tom")
s.Add("Sam")
fmt.Println(s.Has("Tom"))
fmt.Println(s.Has("Jack"))
}
不发送数据的信道channel
- 有时候使用 channel 不需要发送任何的数据,只用来通知子协程(goroutine)执行任务,或只用来控制协程并发度。这种情况下,使用空结构体作为占位符就非常合适了
func worker(ch chan struct{}) {
<-ch
fmt.Println("do something")
close(ch)
}
func main() {
ch := make(chan struct{})
go worker(ch)
ch <- struct{}{}
}
仅包含方法的结构体
- 在部分场景下,结构体只包含方法,不包含任何的字段。
type Door struct{}
func (d Door) Open() {
fmt.Println("Open the door")
}
func (d Door) Close() {
fmt.Println("Close the door")
}
- 如果使用其它类型,如下,不管是int 还是 bool 都会浪费额外的内存
type Door int
type Door bool