0

I am learning https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go#using-a-struct-to-generate-json (old version of Go).

I use go 1.20.1 , Windows 11 x64, GoLand 2022.3.2 .

package sample3

import (
    foo "encoding/json"
    "fmt"
    "time"
)

type myJSON struct {
    IntValue        int       `json:"intValue"`
    BoolValue       bool      `json:"boolValue"`
    StringValue     string    `json:"stringValue"`
    DateValue       time.Time `json:"dateValue"`
    ObjectValue     *myObject `json:"objectValue"`
    NullStringValue *string   `json:"nullStringValue"`
    NullIntValue    *int      `json:"nullIntValue"`
}

type myObject struct {
    ArrayValue []int `json:"arrayValue"`
}

func Main3() {
    otherInt := 4321
    data := &myJSON{
        IntValue:    1234,
        BoolValue:   true,
        StringValue: "hello!",
        DateValue:   time.Date(2022, 3, 2, 9, 10, 0, 0, time.UTC),
        ObjectValue: &myObject{
            ArrayValue: []int{1, 2, 3, 4},
        },
        NullStringValue: nil,
        NullIntValue:    &otherInt,
    }
    fmt.Println(foo.Marshal(data))
    fmt.Println(data)

    type myInt struct {
        IntValue int
    }

    data2 := &myInt{IntValue: 1234}
    fmt.Println(foo.Marshal(data2))

}

line

fmt.Println(foo.Marshal(data))

return

&{1234 true hello! 2022-03-02 09:10:00 +0000 UTC 0xc000008240 <nil> 0xc00001a170}

enter image description here

enter image description here

I want to see {"IntValue": 1234, "BoolValue": true, ...} , please guide me.

full source code https://github.com/donhuvy/vy_learn_go_json2023/blob/main/sample3/main3.go#L36

enter image description here

why I use fmt.Println(string(json.Marshal(data))) causes error?

Vy Do
  • 46,709
  • 59
  • 215
  • 313

2 Answers2

2

I usually use json encoding library. Take a look at below example:

package main

import (
    "encoding/json"
    "time"
)

type myJSON struct {
    IntValue        int       `json:"intValue"`
    BoolValue       bool      `json:"boolValue"`
    StringValue     string    `json:"stringValue"`
    DateValue       time.Time `json:"dateValue"`
    ObjectValue     *myObject `json:"objectValue"`
    NullStringValue *string   `json:"nullStringValue"`
    NullIntValue    *int      `json:"nullIntValue"`
}

type myObject struct {
    ArrayValue []int `json:"arrayValue"`
}

func main() {
    otherInt := 4321
    data := &myJSON{
        IntValue:    1234,
        BoolValue:   true,
        StringValue: "hello!",
        DateValue:   time.Date(2022, 3, 2, 9, 10, 0, 0, time.UTC),
        ObjectValue: &myObject{
            ArrayValue: []int{1, 2, 3, 4},
        },
        NullStringValue: nil,
        NullIntValue:    &otherInt,
    }
    bytes, err := json.Marshal(data)   // <-------------------This line
    println(string(bytes)) // <-------------------And this line
    println(err)
}

Output:

{"intValue":1234,"boolValue":true,"stringValue":"hello!","dateValue":"2022-03-02T09:10:00Z","objectValue":{"arrayValue":[1,2,3,4]},"nullStringValue":null,"nullIntValue":4321}
TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • Your code works! I feel strange, and why I use `fmt.Println(string(json.Marshal(data)))` causes error? See https://user-images.githubusercontent.com/1328316/222613882-180bba68-78fb-41a9-ada1-7d31ad1aa8cf.png – Vy Do Mar 03 '23 at 02:10
  • @RaphaëlColantonio the function `json.Marshal(data)` return two values, the first one is a byte array, the later one is an error which can be `nil`. So, you can not cast result of the function that way. – TaQuangTu Mar 03 '23 at 02:12
  • Seemly `json` standard package updated? Let's see tutorial at https://www.digitalocean.com/community/tutorials/how-to-use-json-in-go#using-a-struct-to-generate-json I understand your explain. Can you confirm with me that json package changed with new versions of go v1.16 --> v.20.1? – Vy Do Mar 03 '23 at 02:13
  • 1
    @RaphaëlColantonio No. They did the same way with me. Just separate the result of `json.Marshal(data)` to two parts (a byte array and an error), then cast the byte array to string, then print it out. – TaQuangTu Mar 03 '23 at 02:15
2

You have not formatter your output properly

do as follows, for detail on fmt check here go_fmt

    d, _ := foo.Marshal(data)
    fmt.Printf("%+v \n", string(d))
    fmt.Printf("%+v \n", *data)

    type myInt struct {
        IntValue int
    }

    data2 := &myInt{IntValue: 1234}
    d2, _ := foo.Marshal(data2)
    fmt.Printf("%+v", string(d2))
recursion
  • 354
  • 6
  • 11