Printing structs; convert structs to JSON format easily.
This file contains 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
// Printing structs. | |
// http://research.swtch.com/gotour | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
type Arc struct { | |
Head string | |
Modifier string | |
} | |
func main() { | |
arc := Arc{"saw", "He"} | |
fmt.Printf("%v\n", arc) | |
fmt.Printf("%+v\n", arc) | |
fmt.Printf("%#v\n", arc) | |
// Convert structs to JSON. | |
data, err := json.Marshal(arc) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s\n", data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works without any extra effort (for private fields too).