Did any developers need code to get a zero value of a struct but without knowing the layout of the struct itself?

The only thing I could come up with was:

func zero[T any](v T) T {
z := &v
zz := reflect.ValueOf(z).Elem()
zz.Set(reflect.Zero(reflect.TypeOf(v)))
return *z
}

And see here an example of usage: go.dev/play/p/Aqzc_nRzOcP

I needed it in order to get zero copies of random structs so I could test that some marshal/unmarshal functionality is a bijection.

Is this a decent way to do it? Are there alternatives?

0

If you have a fediverse account, you can quote this note from your own instance. Search https://metalhead.club/users/mariusor/statuses/115463626219376110 on your instance and quote it. (Note that quoting is not supported in Mastodon.)