How to use Field Tags in the Definition of Struct Type in Golang?

Golang provide structures for the definition of a custom datatype. The concept of structure in Go is similar to the structure in C/C++.

Example:

type Person struct {
    Name    string
    Aadhar   int
    Street  string
    HouseNo int
}

Structures in Golang can be written to files like JSON for storing data on a hard drive or for sending over the network. JSON is a lightweight data storage format. Go provides packages in the standard library to write structures to JSON file and retrieving structures from the JSON file.

During the definition of structures, additional raw string values know as the field tags may be added to the field declaration which is used as the field name in JSON files. If no additional string value i.e field tag is specified, Go uses the default field name which is used to declare the field in the structure.

Structure Definition with Field Tags:

type Person struct {
    Name    string `json:"name"`
    Aadhar  int    `json: "aadhar"`
    Street  string `json: "street"`
    HouseNo int    `json: "house_number"`
}

Note : Field name must start with a capital letter if you want to store structure in JSON.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show how to use Field
// Tags in the Definition of Struct Type
package main
  
import (
    "encoding/json"
    "fmt"
)
  
type Person struct {
    Name    string `json:"name"`         // field tag for Name
    Aadhar  int    `json:"aadhar"`       // field tag for Aadhar
    Street  string `json:"street"`       // field tag for Street
    HouseNo int    `json:"house_number"` // field tag for HouseNO
}
  
func main() {
  
    var p Person
  
    p.Name = "ABCD"
    p.Aadhar = 1234123412341234
    p.Street = "XYZ"
    p.HouseNo = 10
  
    fmt.Println(p)
  
    // returns []byte which is p in JSON form.
    jsonStr, err := json.Marshal(p)
    if err != nil {
        fmt.Println(err.Error())
    }
  
    fmt.Println(string(jsonStr))
  
    // Sample JSON data
    var str = `{ 
        "name" : "PQRX",
        "aadhar" : 1234123412341234,
        "street" : "XYZW",
        "house_number" : 10
    }`
  
    var p2 Person
  
    // retains values of fields from JSON string
    err = json.Unmarshal([]byte(str), &p2)
    // and stores it into p2
    if err != nil {
        fmt.Println(err.Error())
    }
  
    fmt.Println(p2)
}

chevron_right


Output:

{ABCD 1234123412341234 XYZ 10}
{"name":"ABCD","aadhar":1234123412341234,"street":"XYZ","house_number":10}
{PQRX 1234123412341234 XYZW 10}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.