Created
June 12, 2021 23:27
-
-
Save austin-millan/4aee3dbffc9c2c0daa23df9466ed3289 to your computer and use it in GitHub Desktop.
Markdium-
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type A struct { | |
ValA int `json:"valA"` | |
ValB int`json:"valB"` | |
} | |
var input = A{} | |
func main() { | |
dataSequence := [][]byte{ | |
[]byte(`{"valA": 1, "valB": 1}`), | |
[]byte(`{"valA": "invalid", "valB": 3}`), // valA fails to parse | |
[]byte(`{}`), // everything omitted | |
} | |
var As = make([]A, len(dataSequence)) | |
for i, data := range dataSequence { | |
if err := json.Unmarshal(data, &input); err == nil { | |
As[i] = input | |
} else { | |
// skips second element with invalid `valA` | |
} | |
} | |
output, _ := json.Marshal(As[len(As)-2:]) // last two elements | |
fmt.Println(string(output)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment