舉個例子
如果想要獲取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
結構體映射出來,並把私有屬性轉化為公開屬性,那么就能輕松訪問了