Search code examples
goindirection

What's a reason for this function to exist?


In encoding/json the un-exported reflectValue function passes on all its arguments to another function.

func(e *encodeState) reflectValue(v reflect.Value, opts encOpts)       
{           
    valueEncoder(v)(e, v, opts)
}

The call to valueEncoder can be made from wherever reflectValue is called. What's the motivation for this additional function ?


Solution

  • The method can be replaced by direct calls to valueEncoder(v)(e, v, opts). The method is not needed to satisfy an interface, nor is it accessed through reflection.

    The method was much longer back in 2011. The current method may be there because of the past history. It's also possible that the author thought that code readability is improved by encapsulating valueEncoder(v)(e, v, opts) in a method.