Comments (7)
Structs are copied. time.Time is a struct. Just call deepcopy.Copy(). Note that an interface{} is returned so you'll need to assert the copy to its underlying type.
There isn't a way to integrate a custom copy. This should copy anything that is exported, though I'm not sure about funcs and channels as I didn't test those.
from deepcopy.
Yes, but time.Time has unexported fields:
func (s *TestSuite) Test(c *C) {
now := time.Now()
copy, ok := deepcopy.Copy(now).(time.Time)
c.Assert(ok, Equals, true)
c.Assert(now, DeepEquals, copy)
}
... obtained time.Time = time.Time{sec:63611187148, nsec:318097500, loc:(*time.Location)(0x11d2da0)} ("2016-10-04 16:12:28.3180975 +0200 CEST")
... expected time.Time = time.Time{sec:0, nsec:0, loc:(*time.Location)(nil)} ("0001-01-01 00:00:00 +0000 UTC")
For more context, in my application, i use this to copy a struct:
func DeepCopy(dst, src interface{}) interface{} {
buffer := new(bytes.Buffer)
err := gob.NewEncoder(buffer).Encode(src)
if err != nil {
panic(err)
}
err = gob.NewDecoder(buffer).Decode(dst)
if err != nil {
panic(err)
}
return dst
}
It's really slow and drastically affects the application performance. But it works for all types. I'm trying to think if i can add a hook to copy a specific type.
from deepcopy.
Unexported data cannot be copied without using unsafe; this is a limitation of the language. Fields are unexported for a reason. If you try to set an unexported field via package reflect you willl end up with a panic. To confirm you can elide this block of code: https://github.com/mohae/deepcopy/blob/master/deepcopy.go#L72:L74 and see what happens.
You can run the code in this gist: https://gist.github.com/mohae/ab31ae7d2c98c2cbf91f20f7266bb29b and see that deepcopy does what is expected: copies all exported fields in a struct.
If you need to copy unexported fields you're going to have to go the unsafe route.
from deepcopy.
In thinking about it a little more, I think, in regards to time.TIme, you'd have to set the unexported values you care about on the copy by doing the appropriate method calls on the original time.Time value to get the values and setting them on the copy with the appropriate methods.
Maybe this is something that could be handled by deepcopy for time.Time. I could see that being useful but it also could be going down the rabbit hole of special casing various structs in that way instead of just copying the exported fields.
I'm leaning towards that being something that something other than deepcopy should do instead of going down the rabbit hole of special casing behavior for various structs.
You could create a wrapper function that calls deepcopy.Copy()
and then handles any copying of unexported values for the structs you care about.
from deepcopy.
Sorry for yet another post, but I just noticed the Gobs part.
In regards to unexported fields, Gob has the same limitation:
Functions and channels will not be sent in a gob. Attempting to encode such a value at the top level will fail. A struct field of chan or func type is treated exactly like an unexported field and is ignored.
I don't know what your overall use-case is; but there are faster serialization options than Gob. Also, depending on the use-case, re-using the Encoder
and Decoder
might help.
Also, I haven't benchmarked deepcopy; perhaps that's something I should do.
from deepcopy.
Thank you for your response. I think I'm doing a wrapper. I will get back to you if I have something interesting to show.
from deepcopy.
OK, thanks.
I'm going to assume it's ok to close this. Re-open if necessary.
Good luck!
from deepcopy.
Related Issues (15)
- Format strings in test file are invalid HOT 1
- Could Copy(src, dst) be supported? HOT 1
- DeepCopy interface implementation doesn't improve performance
- proposed fix for a nil case HOT 3
- Not an issue, this repo just saved my day.
- Does this repo still being maintained? HOT 2
- Can you provide the performance test data
- panic: reflect: slice index out of range HOT 6
- error can not be copyed HOT 1
- facing issue while call deepcopy HOT 2
- There are cases we cannot deepcopy HOT 4
- Make public copyRecursive(original, cpy reflect.Value) HOT 2
- copy unexported field HOT 2
- Map keys aren't deep-copied HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepcopy.