Go Best Practices
- 不要频繁的使用
<-time.After
,它会创建大量channel和goroutine, 可以使用time.Ticker
类似时间轮的算法解决;
- 不用频繁的创建
[]byte
和 bytes.Buffer
对象, 缓存并重复使用他们;
- 多使用缓存池
sync.Pool
Fasthttp best practices
- Do not allocate objects and
[]byte
buffers - just reuse them as much
as possible. Fasthttp API design encourages this.
- sync.Pool is your best friend.
- Profile your program
in production.
go tool pprof --alloc_objects your-program mem.pprof
usually gives better
insights for optimization opportunities than go tool pprof your-program cpu.pprof
.
- Write tests and benchmarks for hot paths.
- Avoid conversion between
[]byte
and string
, since this may result in memory
allocation+copy. Fasthttp API provides functions for both []byte
and string
-
use these functions instead of converting manually between []byte
and string
.
There are some exceptions - see this wiki page
for more details.
- Verify your tests and production code under
race detector on a regular basis.
- Prefer quicktemplate instead of
html/template in your webserver.