举个例子
如果想要获取http.DefaultServeMux
里面m
怎么办呢?
- 可以通过reflect
- 内存对齐
type ServeMux struct {
Mu sync.RWMutex
M map[string]muxEntry
Es []muxEntry
Hosts bool
}
type muxEntry struct {
H http.Handler
Pattern string
}
func GetDefaultServeMux() *ServeMux {
return (*ServeMux)(unsafe.Pointer(http.DefaultServeMux))
}
把http.ServeMux
结构体映射出来,并把私有属性转化为公开属性,那么就能轻松访问了