Last active
February 24, 2023 11:49
-
-
Save iKlotho/e5f1f800690669eed5658442fd6950b8 to your computer and use it in GitHub Desktop.
Go JSON Decoder Access Size With Custom Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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