This code throws an error:

./prog.go:10:10: struct field Field2 repeats json tag "field_1" also at 
prog.go:10

see https://play.golang.org/p/sFZ-6GWUO0J

Why not use this:

type a_1_struct_name struct {
        Field1 string `json:"field_1"`
        Field2 string `json:"field_2"`
}

________________________________________
Von: golang-nuts@googlegroups.com <golang-nuts@googlegroups.com> im Auftrag von 
amarjeetanandsi...@gmail.com <amarjeetanandsi...@gmail.com>
Gesendet: Freitag, 3. Juli 2020 16:11
An: golang-nuts
Betreff: [go-nuts] ignore json tag for field list in a struct field

If there is a json tag with multiple fields in a single line, is there any json 
parser which will consider the json field name?
Like in below, "encoding/json" ignores the fields to unmarshal if there are 
multiple fields in a single line with a json tag.
EX: below code will print nothing.


type a_1_struct_name struct {
   Field1, Field2 string `json:"field_1"`
}

func main() {
   a := a_1_struct_name{}
   j := `{"field_1": "field_1_val"}`
   if err := json.Unmarshal([]byte(j), &a); err != nil {
      log.Fatal(err)
   }
   fmt.Println(a)
}

But if we remove the json tag `json:"field_1"` and the change the value of 
json(j) accordingly, it prints {field_1_val }

I am writing a program to get the json equivalent tag of struct fields using 
reflect.
My question is, is it safe to ignore the fieldList(comma separated struct 
fields in a single line) if there is a json tag with it?

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com<mailto:golang-nuts+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/1932cc53-b7b6-44d2-b855-1639e4c32ac0o%40googlegroups.com<https://groups.google.com/d/msgid/golang-nuts/1932cc53-b7b6-44d2-b855-1639e4c32ac0o%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/AM7P191MB1060D7B923A795E0455FE6B89E6A0%40AM7P191MB1060.EURP191.PROD.OUTLOOK.COM.

Reply via email to