albertyw/albertyw.com

View on GitHub
app/notes/20170905-0216.md

Summary

Maintainability
Test Coverage
Go Receiver Pointers vs. Values

go-receiver-pointer-values

1504577779

When writing a method in Go, should you use a pointer or a value receiver?

| Type | Use |
|------|---- |
| Basic | Value |
| Map | Value |
| Func | Value |
| Chan | Value |
| Slice (no reslicing/reallocating) | Value |
| Small Struct/Array | Value |
| Concurrent mutations | Value if possible |
| Is Mutated By Method | Pointer |
| Large Struct/Array | Pointer |
| Contains a `sync.Mutex` | Pointer |
| Contains Pointers | Pointer |
| 🤷 | Pointer |

Distilled from [Golang Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments#receiver-type)