How to Convert Go Struct to JSON

How to Convert Go Struct to JSON

JSON is a lightweight language independent format for storing and transporting data. In Go, we have the encoding/json package, which contains many inbuilt methods for JSON related operations. We can convert struct data into JSON using marshaling. Convert Go Struct to JSON The marshal method receives struct object and returns a byte slice of the encoded data, which we can change to JSON in Go. package main import ( "encoding/json" "fmt" ) type Employee struct { Name string `json:"empname"` Number int `json:"empid"` } func main() { employee_1 := &Employee{Name: "Dixya Lhyaho", Number: 10} e, err := json.

Tags

Go String Go JSON Go Struct Go Slice Go Map Go File Go Time Go Installation Go Package Go Array Go Variadic Go Conversion

Most Popular Articles

Recently Updated Articles