Refactor data processing and message formatting

Updated the JSON data structure for better readability and added more comprehensive output formatting in the main function. Utilized a single 'output' string to accumulate messages, improving code readability and efficiency.
This commit is contained in:
Winni Neessen 2024-09-01 17:05:11 +02:00
parent ab747282c8
commit bdd7efec82
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -56,25 +56,36 @@ type JSONType struct {
} }
func main() { func main() {
data := []byte(`{"string":"test", "int":123, "int64": 12345678901234, "float64":0, "nil":null, "bool":true}`) data := []byte(`{
"bytes": "Ynl0ZXM=",
var example JSONType "bool": true,
if err := json.Unmarshal(data, &example); err != nil { "float32": null,
fmt.Println("failed to unmarshal JSON:", err) "float64":0,
os.Exit(1) "int": 123,
} "int64": 12345678901234,
"nilvalue": null,
"string":"test"
}`)
if example.Bool.NotNil() { var example JSONType
fmt.Printf("Bool is: %t\n", example.Bool.Value()) var output string
} if err := json.Unmarshal(data, &example); err != nil {
if example.Float64.IsNil() { fmt.Println("failed to unmarshal JSON:", err)
fmt.Println("Float 32 is NIL") os.Exit(1)
} }
if example.Float64.NotNil() {
fmt.Printf("Float is: %f\n", example.Float64.Value()) if example.Bool.NotNil() {
} output += fmt.Sprintf("Bool is: %t, ", example.Bool.Value())
if !example.String.IsNil() { }
fmt.Printf("String is: %s\n", example.String.Value()) if example.Float32.IsNil() {
} output += "Float 32 is nil, "
}
if example.Float64.NotNil() {
output += fmt.Sprintf("Float 64 is: %f, ", example.Float64.Value())
}
if example.String.NotNil() {
output += fmt.Sprintf("String is: %s", example.String.Value())
}
fmt.Println(output)
} }
``` ```