Last active
March 28, 2019 05:36
-
-
Save ariesmcrae/2691c9eff974a21da614d6bb3fca0c4a to your computer and use it in GitHub Desktop.
golang: Marshal struct to json string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// play.golang.org/p/IAGoPiUdUMc | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type Employee struct { | |
FirstName string `json:"first_name"` | |
LastName string `json:"last_name"` | |
} | |
/* Marshal struct to json string */ | |
func main() { | |
employee := Employee{FirstName: "John", LastName: "Smith"} | |
employeeJsonStr := marshall(employee) | |
fmt.Println(employeeJsonStr) | |
/* Example output | |
{ | |
"first_name":"John", | |
"last_name":"Smith" | |
}*/ | |
} | |
func marshall(employee Employee) string { | |
employeeByte, _ := json.Marshal(employee) | |
return string(employeeByte) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment