I think I remember reading somewhere that the idea of APIs that take a pointer is that you can reuse the MyStruct multiple times to avoid allocations. However, I can't think of a time I ever used the var request MyStruct more than once.
At the moment, you can't have generics on a method, and the design above has DecodeInto being a method on the json.Decoder type which has type parameters.
If some operation has to support even types that don’t have methods (so that interface types don’t help), and if the operation is different for each type (so that type parameters aren’t appropriate), use reflection.
An example of this is the encoding/json package. We don’t want to require that every type that we encode have a MarshalJSON method, so we can’t use interface types. But encoding an interface type is nothing like encoding a struct type, so we shouldn’t use type parameters. Instead, the package uses reflection. The code is not simple, but it works.
I believe with generics now in place a new more convenient
Decodefunction could be added to the
encoding/jsonpackage, amongst other
The benefit of this API is not only is it more concise, but it removes the possibility of a
The text was updated successfully, but these errors were encountered: