mirror of
https://github.com/MADTeacher/go_basics.git
synced 2025-11-29 05:36:55 +02:00
34 lines
683 B
Go
34 lines
683 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
type Actor struct {
|
|
// имя поля при сериализации/десериализации
|
|
Name string `json:"name"`
|
|
Age int `json:"age"`
|
|
// имя тега может отличаться от имени поля структуры
|
|
FilmsAmount int `json:"films_amount"`
|
|
AboutActor *string `json:"about"`
|
|
}
|
|
|
|
func main() {
|
|
aboutStr := "Tom Hanks is an actor..."
|
|
actor := Actor{
|
|
Name: "Tom Hanks",
|
|
Age: 65,
|
|
FilmsAmount: 50,
|
|
AboutActor: &aboutStr,
|
|
}
|
|
|
|
actorJson, err := json.MarshalIndent(actor, "", " ")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(string(actorJson))
|
|
}
|