clarkk
clarkk

Reputation: 27685

print readable variables with golang

How to print a map, struct or whatever in a readable way?

With PHP you can to this

echo '<pre>';
print_r($var);
echo '</pre>';

or

header('content-type: text/plain');
print_r($var);

Upvotes: 28

Views: 79961

Answers (6)

Eefret
Eefret

Reputation: 4774

fmt.Printf("%v", whatever) 

In Go is like print_r(), var_dump(), var_export() in PHP. (The %v is the important part.)

Good Luck

Upvotes: 2

nebiros
nebiros

Reputation: 737

https://github.com/davecgh/go-spew

Go-spew implements a deep pretty printer for Go data structures to aid in debugging.

Upvotes: 3

Kurt Zhong
Kurt Zhong

Reputation: 7486

I think in many cases, using "%v" is concise enough:

fmt.Printf("%v", myVar)

From the fmt package's documentation page:

%v the value in a default format. when printing structs, the plus flag (%+v) adds field names

%#v a Go-syntax representation of the value

Here is an example:

package main

import "fmt"

func main() {
    // Define a struct, slice and map
    type Employee struct {
        id   int
        name string
        age  int
    }
    var eSlice []Employee
    var eMap map[int]Employee

    e1 := Employee{1, "Alex", 20}
    e2 := Employee{2, "Jack", 30}
    fmt.Printf("%v\n", e1)
    // output: {1 Alex 20}
    fmt.Printf("%+v\n", e1)
    // output: {id:1 name:Alex age:20}
    fmt.Printf("%#v\n", e1)
    // output: main.Employee{id:1, name:"Alex", age:20}

    eSlice = append(eSlice, e1, e2)
    fmt.Printf("%v\n", eSlice)
    // output: [{1 Alex 20} {2 Jack 30}]
    fmt.Printf("%#v\n", eSlice)
    // output: []main.Employee{main.Employee{id:1, name:"Alex", age:20}, main.Employee{id:2, name:"Jack", age:30}}

    eMap = make(map[int]Employee)
    eMap[1] = e1
    eMap[2] = e2
    fmt.Printf("%v\n", eMap)
    // output: map[1:{1 Alex 20} 2:{2 Jack 30}]
    fmt.Printf("%#v\n", eMap)
    // output: map[int]main.Employee{1:main.Employee{id:1, name:"Alex", age:20}, 2:main.Employee{id:2, name:"Jack", age:30}}
}

Upvotes: 19

OneOfOne
OneOfOne

Reputation: 99224

For debugging I use this:

func printVars(w io.Writer, writePre bool, vars ...interface{}) {
    if writePre {
        io.WriteString(w, "<pre>\n")
    }
    for i, v := range vars {
        fmt.Fprintf(w, "» item %d type %T:\n", i, v)
        j, err := json.MarshalIndent(v, "", "    ")
        switch {
        case err != nil:
            fmt.Fprintf(w, "error: %v", err)
        case len(j) < 3: // {}, empty struct maybe or empty string, usually mean unexported struct fields
            w.Write([]byte(html.EscapeString(fmt.Sprintf("%+v", v))))
        default:
            w.Write(j)
        }
        w.Write([]byte("\n\n"))
    }
    if writePre {
        io.WriteString(w, "</pre>\n")
    }
}

playground

Upvotes: 1

weberc2
weberc2

Reputation: 7908

You can use fmt.Println() to print. You will need to import the "fmt" package (see the example below). Many data types can be printed out of the box. If you want to get a human-readable print for custom types, you'll need to define a String() string method for that type.

To try the following example, click here: http://play.golang.org/p/M6_KnRJ3Da

package main

import "fmt"

// No `String()` method
type UnstringablePerson struct {
    Age int
    Name string
}

// Has a `String()` method
type StringablePerson struct {
    Age int
    Name string
}

// Let's define a String() method for StringablePerson, so any instances
// of StringablePerson can be printed how we like
func (p *StringablePerson) String() string {
    return fmt.Sprintf("%s, age %d", p.Name, p.Age)
}

func main() {
    // Bobby's type is UnstringablePerson; there is no String() method
    // defined for this type, so his printout will not be very friendly
    bobby := &UnstringablePerson{
        Age: 10,
        Name: "Bobby",
    }

    // Ralph's type is StringablePerson; there *is* a String() method
    // defined for this type, so his printout *will* be very friendly
    ralph := &StringablePerson{
        Age: 12,
        Name: "Ralph",
    }
    fmt.Println(bobby) // prints: &{10 Bobby}
    fmt.Println(ralph) // prints: Ralph, age 12
}

Upvotes: 5

peterSO
peterSO

Reputation: 166569

Use the Go fmt package. For example,

package main

import "fmt"

func main() {
    variable := "var"
    fmt.Println(variable)
    fmt.Printf("%#v\n", variable)
    header := map[string]string{"content-type": "text/plain"}
    fmt.Println(header)
    fmt.Printf("%#v\n", header)
}

Output:

var
"var"
map[content-type:text/plain]
map[string]string{"content-type":"text/plain"}

Package fmt

import "fmt" 

Overview

Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler.

Upvotes: 20

Related Questions