On 7 Aug., 13:17, Manuel Kiessling <
man...@kiessling.net> wrote:
> The things only live in memory, i.e., in a map. In order to make this
> server as efficient as possible memory-wise, I thought it could make sense
> to have this map only store the pointers to the things, but not the things
> itself.
>
> But I'm very new to Go and still don't get the whole "reference" thing,
> thus I'm not sure if the code would be just as efficient if I stored the
> things into the map directly.
This is a question which cannot be answered as the answer depends
on your definition of "efficient". Both variants will use the same
amount
of memory. I wouldn't care about execution time as a pointer
de-reference is negligible compared to the latency of http.
I would recommend: Don't reason about such stuff. Refactor iff
you run into an identifiable issue.
Some remarks on the code:
* Go is smart enough to dereference a pointer: Instead of
(*experiment).Name just write experiment.Name which works
with both: experiment being a types.Experiment or a pointer
to it.
* Instead of
experiment := experiments[url]
if experiment != nil { }
use the comma ok idiom:
if experiment, ok := experiments[url]; ok { }
Again this works for both variants (pointer/value)
* Instead of experiment := new(types.Experiment) I would
use experiment := &types.Experiment{}
With these little changes your code will be almost exactly
the same, regardless whether you store experiment values
or experiment pointers in your map. Refactoring will be
done in 2 minutes (if needed).
Volker