print readable variables with golang

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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

回答1:

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.



回答2:

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


回答3:

fmt.Printf("%v", whatever)  

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

Good Luck



回答4:

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 } 


回答5:

For debugging I use this:

playground



回答6:

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!