Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Last active February 24, 2023 11:49
Show Gist options
  • Save iKlotho/e5f1f800690669eed5658442fd6950b8 to your computer and use it in GitHub Desktop.
Save iKlotho/e5f1f800690669eed5658442fd6950b8 to your computer and use it in GitHub Desktop.
Go JSON Decoder Access Size With Custom Reader
type LengthReader struct {
io.Reader
Length int
}
func (cw *LengthReader) Read(p []byte) (n int, err error) {
n, err = cw.Reader.Read(p)
cw.Length += n
return n, err
}
v := interface{}
lr := &LengthReader{Reader: r.Body}
decoder := json.NewDecoder(lr).Decode(&v)
fmt.Printf("The size of the payload is %d \n", lr.Length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment