Newbie syntax question

Hello all,

I’m trying to understand the syntax for this struct. What is new for me is the json tag at the end? Is that specific to json? Never encountered that before…and the tour to go doesn’t seem to have touched upon it, or I missed/forgot it

type response2 struct {
Page int json:"page"
Fruits []string json:"fruits"
}

The parts I do understand is that, we are creating a custom type, with name response2 that is a struct. Struct has two elements:
variable Page of type int
variable Fruits of type []string

What’s the json string?

Hi gt33m,

You can check this link for reference: https://golang.org/pkg/encoding/json/
It explained the meaning.

2 Likes

ah, thanks. I’ll read that.

That link specifically for understanding of json tag for struct. the tag that you mentioned is just a tag. for e.g you initialized an object without value it will return the name tag you assigned.

Thanks Adriel.

Is this tag syntax specific to structs? Can it be used in other cases too

I found this (https://medium.com/golangspec/tags-in-golang-3e5db0b8ef3e) an easier read :wink: May be useful to others that had the same question as I had.

Never mind, the blog I referenced answered my own question.

A field declaration may be followed by an optional string literal ( tag) which becomes an attribute of all the fields in the corresponding field declaration (single field declaration can specify multiple identifiers).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.