Created
June 5, 2021 15:37
-
-
Save viggy28/c08bf1a5a5f23a7dcfe2226c45925b0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// There are two ways I can convert json body to struct. Don't know what's the difference. | |
// https://www.reddit.com/r/golang/comments/5yhfo1/jsondecoder_vs_jsonunmarshal/ | |
// Other than reddit reference I don't see any other links. | |
//Method1: | |
var c Service | |
byteArray, err := ioutil.ReadAll(req.Body) | |
if err != nil { | |
log.Fatalf("fatal: reading from readall body %v", req.Body) | |
} | |
err = json.Unmarshal(byteArray, &c) | |
if err != nil { | |
log.Fatalf("fatal: reading from readall body %v", req.Body) | |
} | |
//Method2: | |
var c Service | |
err = json.NewDecoder(req.Body).Decode(&c) | |
if err != nil { | |
log.Fatalf("fatal: reading request body %v", req.Body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@theckman in Golang slack community responded with: