Post AUqFGtO2q5nF04BPUm by galdor@emacs.ch
 (DIR) More posts by galdor@emacs.ch
 (DIR) Post #AUqEBKckYjWdmbqVKy by louis@emacs.ch
       2023-04-20T11:09:39Z
       
       0 likes, 0 repeats
       
       Very unfortunate that in Go when you have a type definition liketype MyDate time.Timeyou loose all the methods of time.Time, including un-/marshalling, while on the other side you cannot define your own methods on non-local types (like time.Time).Am I overlooking something or is this just a bad design choice?#golang #go
       
 (DIR) Post #AUqFGtO2q5nF04BPUm by galdor@emacs.ch
       2023-04-20T11:21:52Z
       
       0 likes, 0 repeats
       
       @louis You want the following:type MyDate = time.TimeSee https://go.dev/ref/spec#Type_declarations.
       
 (DIR) Post #AUqFkXuBGvbf3zM1lQ by cobratbq@mastodon.social
       2023-04-20T11:27:14Z
       
       0 likes, 0 repeats
       
       @galdor @louis true if you don't need additional methods. This is a alias.You could also do struct embedding without a field name. It would make all functions transparently available.
       
 (DIR) Post #AUqH3Pg2BjWP05IWpc by markus@hachyderm.io
       2023-04-20T11:41:50Z
       
       0 likes, 0 repeats
       
       @louis It sounds like you're basically wanting inheritance? That's very deliberately not a thing in Go.
       
 (DIR) Post #AUqIxaqJxcKDnlShbk by louis@emacs.ch
       2023-04-20T12:02:43Z
       
       0 likes, 0 repeats
       
       @galdor Nope, won't work. You still cannot define your own methods on type aliases.
       
 (DIR) Post #AUqJDfVYNr74u4Ph9k by shuLhan@fosstodon.org
       2023-04-20T12:06:07Z
       
       0 likes, 0 repeats
       
       @louis IME, I usually use `type Y X` for native type, like `type MyKind int`.For map, slice or struct, I usually use embedding,type Y struct { X }
       
 (DIR) Post #AUqJHLSarYDdsumYCG by galdor@emacs.ch
       2023-04-20T12:06:42Z
       
       0 likes, 0 repeats
       
       @louis You are correct, my bad. The point of using a type alias is that you can use a value of type `time.Time` where `MyDate` is expected.You indeed need to wrap it:type MyDate struct {time.Time}Which lets you add methods (but forces wrapping).Go is really not good at data modeling infortunately.
       
 (DIR) Post #AUqKWnSd8hVr1bjNwG by serpentroots@hachyderm.io
       2023-04-20T12:20:46Z
       
       0 likes, 0 repeats
       
       @louis it's a deliberate design choice in Go to avoid inheritance and some of the messier aspects of OO.You can, as others have pointed out, wrap your datatype in a struct and write extention funcs on that.You can also just create simple functions that take time.Time as an argument and return something. Small composable functions over large types is Go's superpower.