879 points by golangguru 6 months ago flag hide 11 comments
jdoe 6 months ago next
Great post, thanks for sharing. I've been working on optimizing a Go program recently, and I've found that profiling with pprof has been invaluable.
hnuser2 6 months ago next
Agreed, +1 for pprof. Also, using the -race flag can help catch any race conditions early on.
jdoe 6 months ago prev next
That's a great point. I've noticed that my program was allocating a lot of memory when iterating through a large data set. I'll try to use slices and pointers instead.
devguru 6 months ago prev next
I've found that avoiding allocations as much as possible can make a huge difference in performance. Using slices and allocated pointers can help significantly.
langfan 6 months ago prev next
Another optimization trick I've learned is to use buffered I/O in Go. This can improve the performance of programs that need to write a lot of data to disk.
hnuser1 6 months ago next
Yes, buffered I/O is definitely a must-know technique in Go. It's important to choose the right buffer size depending on the specific use case. Thank you for sharing!
codewhiz 6 months ago prev next
For those of you who are interested in learning more about optimizing Go programs, there's a great talk by Katherine Cox-Buday from GopherCon 2017. It covers a lot of the topics in this post and more.
jdoe 6 months ago next
Thanks for the recommendation, I'll definitely check it out! I've also found that using concurrency primitives like goroutines and channels can improve performance by allowing parallel execution of tasks.
gitmaven 6 months ago prev next
I'm new to Go programming, and I'm wondering if there are any common performance traps that beginners should be aware of. Any advice is appreciated.
hnuser3 6 months ago next
I'm also a beginner, but I've learned that allocating memory with the new keyword can be expensive in terms of performance. It's better to use pools or slices if possible.
goexpert 6 months ago prev next
One common mistake I see is when developers use the range keyword to iterate through a slice of structs, and they allocate memory for each iteration. This can be a performance killer. Instead, it's better to pre-allocate the memory for the slice and then use indexing to avoid unnecessary allocations.