print readable variables with golang

后端 未结 6 519

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

With PHP you can to this

echo \'
\';
print_r($var);
echo \'
\';

6条回答
  •  执笔经年
    2021-02-04 03:09

    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.

提交回复
热议问题