go: printing struct & array

Printing struct https://play.golang.org/p/BvMIqF3Pmc9

package main

import "fmt"

type point struct {
    x, y int
}

func main() {
    p := point{1, 2}
    fmt.Println("Printing struct point:")
    fmt.Printf(" type          %%T: %T\n", p)
    fmt.Printf(" just values   %%v: %v\n", p)
    fmt.Printf(" +field names %%+v: %+v\n", p)
    fmt.Printf(" go syntax    %%#v: %#v\n", p)
    fmt.Println()

    ptr := &p
    fmt.Println("Printing pointer to struct point:")
    fmt.Printf(" type          %%T: %T\n", ptr)
    fmt.Printf(" just values   %%v: %v\n", ptr)
    fmt.Printf(" +field names %%+v: %+v\n", ptr)
    fmt.Printf(" go syntax    %%#v: %#v\n", ptr)
    fmt.Println()

    arrP := []point{point{1, 2},point{3, 4}}
    fmt.Println("Printing array of struct points:")
    fmt.Printf(" type          %%T: %T\n", arrP)
    fmt.Printf(" just values   %%v: %v\n", arrP)
    fmt.Printf(" +field names %%+v: %+v\n", arrP)
    fmt.Printf(" go syntax    %%#v: %#v\n", arrP)
}

Output:

Printing struct point:
 type          %T: main.point
 just values   %v: {1 2}
 +field names %+v: {x:1 y:2}
 go syntax    %#v: main.point{x:1, y:2}

Printing pointer to struct point:
 type          %T: *main.point
 just values   %v: &{1 2}
 +field names %+v: &{x:1 y:2}
 go syntax    %#v: &main.point{x:1, y:2}

Printing array of struct points:
 type          %T: []main.point
 just values   %v: [{1 2} {3 4}]
 +field names %+v: [{x:1 y:2} {x:3 y:4}]
 go syntax    %#v: []main.point{main.point{x:1, y:2}, main.point{x:3, y:4}}
This entry was posted in golang, workday. Bookmark the permalink.

Leave a Reply