Use Field Tags in the Definition of Struct Type
During the definition of a struct type, optional string values may be added to each field declaration.
Example
package main
import (
"fmt"
"encoding/json"
)
type Employee struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
City string `json:"city"`
}
func main() {
json_string := `
{
"firstname": "Rocky",
"lastname": "Sting",
"city": "London"
}`
emp1 := new(Employee)
json.Unmarshal([]byte(json_string), emp1)
fmt.Println(emp1)
emp2 := new(Employee)
emp2.FirstName = "Ramesh"
emp2.LastName = "Soni"
emp2.City = "Mumbai"
jsonStr, _ := json.Marshal(emp2)
fmt.Printf("%s\n", jsonStr)
}
Most Helpful This Week
How to slice elements in Golang?
How do you create an HTTP server in Go?
Write string slice line by line to a text file
How to delete an element from a Slice in Golang?
How do you send an HTTP PUT request in Go?
Golang program for implementation of Tower of Hanoi Algorithm
Implementing Multiple Interfaces in Go Programming Language
Sierpinski Carpet in Go Programming Language
Golang Program to Triangle of Alphabets
Golang program for implementation of Interpolation Search