举个例子

如果想要获取http.DefaultServeMux里面m怎么办呢?

  1. 可以通过reflect
  2. 内存对齐
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结构体映射出来,并把私有属性转化为公开属性,那么就能轻松访问了

相关文章:

  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-07-15
  • 2021-07-07
  • 2021-05-26
  • 2022-01-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-11-08
  • 2021-12-20
相关资源
相似解决方案