与你常在

两种方法对比:

1、gohttp包的方式,看起来挺奇妙

代码:

1
2
3
4

	es = append(es, muxEntry{}) // try to grow the slice in place, any entry works.
	copy(es[i+1:], es[i:])      // Move shorter entries down
	es[i] = e
  • 分别使用了append方法预先扩容一个位置
  • 然后使用copy函数拷贝数据

2、单用append的方法,也是之前常用的方法

1
es = append(es[:i], e, es[i+1:])
  • append (len-i次),每次都会检查是否需要扩容。