DEV Community

あとやす / a_yasui
あとやす / a_yasui

Posted on

[memo] Debug values at Golang

#go

I forget these.

package main

import (
    "github.com/davecgh/go-spew/spew"
    "log"
)

type Name struct {
    FirstName string
    LastName  string
}

type Address struct {
    Country    string
    PostalCode string
    Address    string
}

type Person struct {
    Name    Name
    Address Address
}

func main() {
    person := &Person{
        Name{"Cynthia", "Stone"},
        Address{"us", "02110", "1091 Lyon Avenue, Boston"},
    }

    log.Printf("[debug] %+v", person)

    log.Print("[debug] Humanize Output")
    spew.Dump(person)
}

execute output :

2009/11/10 23:00:00 [debug] &{Name:{FirstName:Cynthia LastName:Stone} Address:{Country:us PostalCode:02110 Address:1091 Lyon Avenue, Boston}}
2009/11/10 23:00:00 [debug] Humanize Output
(*main.Person)(0xc0000c6140)({
 Name: (main.Name) {
  FirstName: (string) (len=7) "Cynthia",
  LastName: (string) (len=5) "Stone"
 },
 Address: (main.Address) {
  Country: (string) (len=2) "us",
  PostalCode: (string) (len=5) "02110",
  Address: (string) (len=24) "1091 Lyon Avenue, Boston"
 }
})

see : Go言語でPHPのvar_dumpやJavaScriptのconsole.log的なもの -- Qiita

Top comments (0)