Probably not clear from the question itself what I'm after here, so let me clarify. As an exercise in concurrency, I'm playing around with writing a cache that needs to be accessible by multiple simultaneous requests. Cache content is of type interface{}, so it can include anything, including slices, maps, and structs. When I grab something with a Get method, I RLock on it while reading it and then return the content and finish with a deferred RUnlock.
This works fine for numbers and strings and any other values that are automatically copied on return. But I'm concerned that slices, maps, and structs are not actually copied, such that the thing returned, if read or modified as if it were a copy, would actually be altering data in the cache and doing so outside of a mutex.
Of course, that's a problem under race conditions. So I don't want to return something from Get that is not safe to alter and then pass back to a Set method to update. So here are the questions:
1) Am I correct in assuming that these data types present problems for a scenario like this?
2) How might one go about resolving this issue, so as to create a Get method whose values can be freely manipulated without fear of failure under race conditions?