On Saturday, July 4, 2020 at 12:25:30 AM UTC+5:30, Lutz Horn wrote:
>
> 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
>
ohh
Yeah, its failing on go 14, but it runs in my local on go 13.
So probably I shouldn't be worried about dropping the fields from output
list.
> Why not use this:
>
> type a_1_struct_name struct {
> Field1 string `json:"field_1"`
> Field2 string `json:"field_2"`
> }
>
> Yeah this would be the proper way to write json tags. But I am writing a
parser kind of tool that will try to generate the filed tags of struct. So
I never know how somebody else is going to write their program that I am
going to parse.
________________________________________
> Von: [email protected] <javascript:> <[email protected]
> <javascript:>> im Auftrag von [email protected] <javascript:> <
> [email protected] <javascript:>>
> 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 [email protected] <javascript:><mailto:
> [email protected] <javascript:>>.
> 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/6113cca9-b562-4725-8734-232ace5fff16o%40googlegroups.com.