使用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);

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
}

Use the Go fmt package. For example,

package main

import "fmt"

func main() {
    variable := "var"
    fmt.Println(variable)
    fmt.Printf("%#v
", variable)
    header := map[string]string{"content-type": "text/plain"}
    fmt.Println(header)
    fmt.Printf("%#v
", 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.

For debugging I use this:

func printVars(w io.Writer, writePre bool, vars ...interface{}) {
    if writePre {
        io.WriteString(w, "<pre>
")
    }
    for i, v := range vars {
        fmt.Fprintf(w, "» item %d type %T:
", 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("

"))
    }
    if writePre {
        io.WriteString(w, "</pre>
")
    }
}

playground

fmt.Printf("%v", whatever) 

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

Good Luck

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
", e1)
    // output: {1 Alex 20}
    fmt.Printf("%+v
", e1)
    // output: {id:1 name:Alex age:20}
    fmt.Printf("%#v
", e1)
    // output: main.Employee{id:1, name:"Alex", age:20}

    eSlice = append(eSlice, e1, e2)
    fmt.Printf("%v
", eSlice)
    // output: [{1 Alex 20} {2 Jack 30}]
    fmt.Printf("%#v
", 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
", eMap)
    // output: map[1:{1 Alex 20} 2:{2 Jack 30}]
    fmt.Printf("%#v
", eMap)
    // output: map[int]main.Employee{1:main.Employee{id:1, name:"Alex", age:20}, 2:main.Employee{id:2, name:"Jack", age:30}}
}

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

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