4 ms·
You should use pointers if you need to mutate the content of the struct, and values otherwise. Almost all types that could hold a lot of data have reference se
by sagichmal 7y ago
You should use pointers if you need to mutate the content of the struct, and values otherwise.
Almost all types that could hold a lot of data have reference semantics, so if you have a struct with a large slice, or a large map, all of the actual data is going to be on the heap, and not affect copy performance.
The only time you might want to consider using a pointer to improve copy performance is if you have lots of non-heap-allocated data in your struct, and in practice the only way that happens is via large arrays. That is, e.g. `[8192]string`, and not simply `[]string` (which is an efficient slice). But you should never do it preemptively, you should always benchmark both styles and only switch to pointers once you have proof it's meaningfully affecting performance.