ZhangZhihui's Blog  
package main

import (
    "fmt"
)

type Person struct {
    Name  string
    Age   int
    Email string
}

func main() {
    p := Person{
        Name:  "Drake",
        Age:   35,
        Email: "drake@example.com",
    }

    fmt.Println(p)
    fmt.Println("------------------------------------------------------------")
    fmt.Printf("%+v\n", p)
    fmt.Println("------------------------------------------------------------")
    fmt.Printf("%#v\n", p)
    fmt.Println("------------------------------------------------------------")
    fmt.Printf("%# v\n", p)
}

 

zzh@ZZHPC:/zdata/MyPrograms/Go/study$ go run main.go
{Drake 35 drake@example.com}
------------------------------------------------------------
{Name:Drake Age:35 Email:drake@example.com}
------------------------------------------------------------
main.Person{Name:"Drake", Age:35, Email:"drake@example.com"}
------------------------------------------------------------
main.Person{Name:"Drake", Age: 35, Email:"drake@example.com"}

 

Pretty look:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    type Person struct {
        Name  string
        Age   int
        Email string
    }

    p := Person{
        Name:  "Drake",
        Age:   35,
        Email: "drake@example.com",
    }

    bs, _ := json.MarshalIndent(p, "", "\t")
    fmt.Println(string(bs))
}

 

zzh@ZZHPC:/zdata/MyPrograms/Go/study$ go run main.go
{
        "Name": "Drake",
        "Age": 35,
        "Email": "drake@example.com"
}

 

posted on 2023-10-23 20:38  ZhangZhihuiAAA  阅读(8)  评论(0编辑  收藏  举报