csdnceshi78
2014-07-01 13:57 阅读 242

如何在控制台中打印结构变量?

How can I print (in the console) the Id, Title, Name, etc. of this struct in Golang?

type Project struct {
    Id int64 `json:"project_id"`
    Title string `json:"title"`
    Name string `json:"name"`
    Data Data `json:"data"`
    Commits Commits `json:"commits"`
}

转载于:https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

14条回答 默认 最新

  • 已采纳
    csdnceshi75 衫裤跑路 2014-07-01 14:00

    To print the name of the fields in a struct:

    fmt.Printf("%+v\n", yourProject)
    

    From the fmt package:

    when printing structs, the plus flag (%+v) adds field names

    That supposes you have an instance of Project (in 'yourProject')

    The article JSON and Go will give more details on how to retrieve the values from a JSON struct.


    This Go by example page provides another technique:

    type Response2 struct {
      Page   int      `json:"page"`
      Fruits []string `json:"fruits"`
    }
    
    res2D := &Response2{
        Page:   1,
        Fruits: []string{"apple", "peach", "pear"}}
    res2B, _ := json.Marshal(res2D)
    fmt.Println(string(res2B))
    

    That would print:

    {"Page":1,"Fruits":["apple","peach","pear"]}
    

    If you don't have any instance, then you need to use reflection to display the name of the field of a given struct, as in this example.

    type T struct {
        A int
        B string
    }
    
    t := T{23, "skidoo"}
    s := reflect.ValueOf(&t).Elem()
    typeOfT := s.Type()
    
    for i := 0; i < s.NumField(); i++ {
        f := s.Field(i)
        fmt.Printf("%d: %s %s = %v\n", i,
            typeOfT.Field(i).Name, f.Type(), f.Interface())
    }
    
    点赞 28 评论 复制链接分享
  • weixin_41568174 from.. 2017-03-29 08:53

    I think it would be better to implement a custom stringer if you want some kind of formatted output of a struct

    for example

    package main
    
        import "fmt"
    
        type Project struct {
            Id int64 `json:"project_id"`
            Title string `json:"title"`
            Name string `json:"name"`
        }
    
        func (p Project) String() string {
            return fmt.Sprintf("{Id:%d, Title:%s, Name:%s}", p.Id, p.Title, p.Name)
        }
    
        func main() {
            o := Project{Id: 4, Name: "hello", Title: "world"}
            fmt.Printf("%+v\n", o)
        }
    
    点赞 10 评论 复制链接分享
  • csdnceshi78 程序go 2015-12-27 11:33

    I want to recommend go-spew, which according to their github "Implements a deep pretty printer for Go data structures to aid in debugging"

    go get -u github.com/davecgh/go-spew/spew
    

    usage example:

    package main
    
    import (
        "github.com/davecgh/go-spew/spew"
    )
    
    type Project struct {
        Id      int64  `json:"project_id"`
        Title   string `json:"title"`
        Name    string `json:"name"`
        Data    string `json:"data"`
        Commits string `json:"commits"`
    }
    
    func main() {
    
        o := Project{Name: "hello", Title: "world"}
        spew.Dump(o)
    }
    

    output:

    (main.Project) {
     Id: (int64) 0,
     Title: (string) (len=5) "world",
     Name: (string) (len=5) "hello",
     Data: (string) "",
     Commits: (string) ""
    }
    
    点赞 8 评论 复制链接分享
  • csdnceshi67 bug^君 2018-02-16 11:29

    I like litter.

    From their readme:

    type Person struct {
      Name   string
      Age    int
      Parent *Person
    }
    
    litter.Dump(Person{
      Name:   "Bob",
      Age:    20,
      Parent: &Person{
        Name: "Jane",
        Age:  50,
      },
    })
    

    Sdump is pretty handy in tests:

    func TestSearch(t *testing.T) {
      result := DoSearch()
    
      actual := litterOpts.Sdump(result)
      expected, err := ioutil.ReadFile("testdata.txt")
      if err != nil {
        // First run, write test data since it doesn't exist
            if !os.IsNotExist(err) {
          t.Error(err)
        }
        ioutil.Write("testdata.txt", actual, 0644)
        actual = expected
      }
      if expected != actual {
        t.Errorf("Expected %s, got %s", expected, actual)
      }
    }
    
    点赞 8 评论 复制链接分享
  • csdnceshi74 7*4 2018-09-20 09:59

    I recommend to use Pretty Printer Library. In that you can print any struct very easily.

    1. Install Library

      https://github.com/kr/pretty

    or

    go get github.com/kr/pretty
    

    Now do like this in your code

    package main
    
    import (
    fmt
    github.com/kr/pretty
    )
    
    func main(){
    
    type Project struct {
        Id int64 `json:"project_id"`
        Title string `json:"title"`
        Name string `json:"name"`
        Data Data `json:"data"`
        Commits Commits `json:"commits"`
    }
    
    fmt.Printf("%# v", pretty.Formatter(Project)) //It will print all struct details
    
    fmt.Printf("%# v", pretty.Formatter(Project.Id)) //It will print component one by one.
    
    }
    

    Also you can get difference between component through this library and so more. You can also have a look on library Docs here.

    点赞 8 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 2016-06-15 20:42

    There's also go-render, which handles pointer recursion and lots of key sorting for string and int maps.

    Installation:

    go get github.com/luci/go-render/render
    

    Example:

    type customType int
    type testStruct struct {
            S string
            V *map[string]int
            I interface{}
    }
    
    a := testStruct{
            S: "hello",
            V: &map[string]int{"foo": 0, "bar": 1},
            I: customType(42),
    }
    
    fmt.Println("Render test:")
    fmt.Printf("fmt.Printf:    %#v\n", a)))
    fmt.Printf("render.Render: %s\n", Render(a))
    

    Which prints:

    fmt.Printf:    render.testStruct{S:"hello", V:(*map[string]int)(0x600dd065), I:42}
    render.Render: render.testStruct{S:"hello", V:(*map[string]int){"bar":1, "foo":0}, I:render.customType(42)}
    
    点赞 4 评论 复制链接分享
  • csdnceshi53 Lotus@ 2016-12-18 04:14

    Visit here to see the complete code. Here you will also find a link for an online terminal where the complete code can be run and the program represents how to extract structure's information(field's name their type & value). Below is the program snippet that only prints the field names.

    package main
    
    import "fmt"
    import "reflect"
    
    func main() {
        type Book struct {
            Id    int
            Name  string
            Title string
        }
    
        book := Book{1, "Let us C", "Enjoy programming with practice"}
        e := reflect.ValueOf(&book).Elem()
    
        for i := 0; i < e.NumField(); i++ {
            fieldName := e.Type().Field(i).Name
            fmt.Printf("%v\n", fieldName)
        }
    }
    
    /*
    Id
    Name
    Title
    */
    
    点赞 4 评论 复制链接分享
  • weixin_41568184 叼花硬汉 2017-06-03 03:09
    p = Project{...}
    fmt.Printf("%+v", p)
    fmt.Printf("%#v", p) //with type
    
    点赞 3 评论 复制链接分享
  • csdnceshi74 7*4 2018-07-10 16:24

    my 2cents would be to use json.MarshalIndent -- surprised this isn't suggested, as it is the most straightforward. for example:

    func prettyPrint(i interface{}) string {
        s, _ := json.MarshalIndent(i, "", "\t")
        return string(s)
    }
    

    no external deps and results in nicely formatted output.

    点赞 1 评论 复制链接分享
  • csdnceshi55 ~Onlooker 2017-04-18 10:22

    Another way is, create a func called toString that takes struct, format the fields as you wish.

    import (
        "fmt"
    )
    
    type T struct {
        x, y string
    }
    
    func (r T) toString() string {
        return "Formate as u need :" + r.x + r.y
    }
    
    func main() {
        r1 := T{"csa", "ac"}
        fmt.Println("toStringed : ", r1.toString())
    }
    
    点赞 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 2017-06-16 14:54

    I want to recommend Stuct Example GO language program with an example of Struc Type.

    package main
    
    import (
    "fmt"
    )
    
    func main() {
        type Salary struct{
            Basic, HRA, TA float64      
        }
    
        type Employee struct{
            FirstName, LastName, Email string
            Age int
            MonthlySalary []Salary
        }
    
        e := Employee{
            FirstName: "Mark",
            LastName: "Jones",
            Email: "mark@gmail.com",
            Age: 25,
            MonthlySalary: []Salary{
                Salary{
                    Basic:15000.00,
                    HRA:5000.00,
                    TA:2000.00,
                },
                Salary{
                    Basic:16000.00,
                    HRA:5000.00,
                    TA:2100.00,
                },
                Salary{
                    Basic:17000.00,
                    HRA:5000.00,
                    TA:2200.00,
                },
            },
        }
        fmt.Println(e.FirstName,e.LastName)
        fmt.Println(e.Age)
        fmt.Println(e.Email)
        fmt.Println(e.MonthlySalary[0])
        fmt.Println(e.MonthlySalary[1])
        fmt.Println(e.MonthlySalary[2])
    }
    
    点赞 评论 复制链接分享
  • weixin_41568110 七度&光 2017-10-21 08:13

    Without using external libraries and with new line after each field:

    log.Println(
                strings.Replace(
                    fmt.Sprintf("%#v", post), ", ", "\n", -1))
    
    点赞 评论 复制链接分享
  • csdnceshi64 游.程 2018-07-23 14:33

    When you have more complex structures, you might need to convert to JSON before printing:

    // Convert structs to JSON.
    data, err := json.Marshal(myComplexStruct)
    fmt.Printf("%s\n", data)
    

    Source: https://gist.github.com/tetsuok/4942960

    点赞 评论 复制链接分享
  • csdnceshi62 csdnceshi62 2018-10-26 13:44
    fmt.Println("%+v", structure variable)
    

    A better way to do this would be to create a global constant for the string "%+v" in a package called "commons"(maybe) and use it everywhere in your code

    //In commons package
    const STRUCTURE_DATA_FMT = "%+v"
    
    //In your code everywhere
    fmt.Println(commons.STRUCTURE_DATA_FMT, structure variable)
    
    点赞 评论 复制链接分享

相关推荐