Print readable variables with golang

How to print a map, structure or something else in a readable way?

With PHP you can do it

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

or

 header('content-type: text/plain'); print_r($var); 
+7
go
source share
6 answers

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"} 

Fmt package

 import "fmt" 

Overview

The fmt package implements formatted I / O with functions similar to C printf and scanf. The format of "verbs" is derived from C, but simpler.

+6
source share

I think in many cases the use of "% v" is concise enough:

 fmt.Printf("%v", myVar) 

On the fmt package documentation page:

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

% # v a-syntax representation of a 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}} } 
+2
source share
 fmt.Printf("%v", whatever) 

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

Luck

+1
source share

You can use fmt.Println() to print. You will need to import the "fmt" package (see Example below). Many types of data can be printed out of the box. If you want a readable font for custom types, you need to define the 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 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 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 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 } 
0
source share

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

0
source share

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

Go-spew implements a very beautiful printer for Go data structures to help debug.

0
source share

All Articles