You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are all used to writing these four lines of code:
var name MyStruct
if err := json.Unmarshal(someBytes, &name); err != nil {
...
}
This was fine when Go was introduced, but nowadays with generics the standard library feels like it could be more ergonomic by replacing or adding a new Unmarshal func that can directly return a new object of some type without first declaring a zero value of that type.
This would have at least one limitation that I would find problematic: Such a function would not allow unmarshaling into a pre-populated data structure.
It's not quite an apples-to-apples comparison if the error checking is kept in one example, but not the other.
In both cases, it's still 4 lines.
As @flimzy mentioned, we still need the non-generic variant for unmarshaling into a pre-populated value. Given that the generic variant doesn't provide much benefit, such proposals haven't quite stuck. For example, see golang/go#59053.
We are all used to writing these four lines of code:
This was fine when Go was introduced, but nowadays with generics the standard library feels like it could be more ergonomic by replacing or adding a new
Unmarshal
func that can directly return a new object of some type without first declaring a zero value of that type.The text was updated successfully, but these errors were encountered: