Delve into a community where programmers unite to discover code snippets, exchange skills, and enhance their programming proficiency. With abundant resources and a supportive community, you'll find everything essential for your growth and success.
package main
import("fmt""encoding/json")type User struct{ FirstName string`json:"firstname"` SecondName string`json:"secondname"` Salary int`json:"salary"`}funcmain(){ user := User{ FirstName:"John", SecondName:"Doe", Salary:5000,} b, err := json.MarshalIndent(user,""," ")if err !=nil{ fmt.Println(err)} fmt.Print(string(b))}
We can utilize the `json.MarshalIndent` function, which requires the interface we want to marshal, along with a prefix string and an indent string. In this case, we won't use a prefix, but we will set the indent to two empty spaces.