Home > Backend Development > Golang > How can I easily print Go structs using their fields' String() methods without implementing String() for every struct?

How can I easily print Go structs using their fields' String() methods without implementing String() for every struct?

Linda Hamilton
Release: 2024-12-14 10:52:14
Original
297 people have browsed it

How can I easily print Go structs using their fields' String() methods without implementing String() for every struct?

Printing Struct Fields with String()

This code:

package main

import (
    "fmt"
    "time"
)

type A struct {
    t time.Time
}

func main() {
    a := A{time.Now()}
    fmt.Println(a)
    fmt.Println(a.t)
}
````
generates this output:
Copy after login

{{63393490800 0 0x206da0}}
2009-11-10 23:00:00 0000 UTC

`A` doesn't implement `String()`, so it doesn't conform to the `fmt.Stringer` interface and presents its internal representation. Implementing `String()` for every struct can be tedious, especially upon modifications to those structs. Is there a simpler method to print structs using their fields' `String()` methods?

**The Custom Print Function**

The fmt package's behavior in this case is intrinsic and cannot be changed. However, a helper function can be crafted using reflection (provided by the `reflect` package). The function iterates through a struct's fields and invokes their `String()` methods if they exist:
Copy after login

func PrintStruct(s interface{}, names bool) string {

v := reflect.ValueOf(s)
t := v.Type()
if t.Kind() != reflect.Struct {
    return fmt.Sprint(s)
}

var b bytes.Buffer
b.WriteString("{")
for i := 0; i < v.NumField(); i++ {
    if i > 0 {
        b.WriteString(" ")
    }
    v2 := v.Field(i)
    if names {
        b.WriteString(t.Field(i).Name)
        b.WriteString(":")
    }
    if v2.CanInterface() {
        if st, ok := v2.Interface().(fmt.Stringer); ok {
            b.WriteString(st.String())
            continue
        }
    }
    fmt.Fprint(&b, v2)
}
b.WriteString("}")
return b.String()
Copy after login

}

**Integrating the Helper Function**

With the custom function in place, structs can be printed by:

```go
fmt.Println(PrintStruct(a, true))
Copy after login

If desired, a String() method can be added to a struct, invoking the PrintStruct() function:

func (a A) String() string {
    return PrintStruct(a, true)
}
Copy after login

Since we employ reflection, it's crucial to export struct fields for accessibility by the custom function (added additional fields for testing purposes):

type A struct {
    T          time.Time
    I          int
    unexported string
}
Copy after login

Practical Example

The complete code:

package main

import (
    "fmt"
    "reflect"
    "strings"
    "time"
)

type A struct {
    T          time.Time
    I          int
    unexported string
}

func PrintStruct(s interface{}, names bool) string {
    v := reflect.ValueOf(s)
    t := v.Type()
    if t.Kind() != reflect.Struct {
        return fmt.Sprint(s)
    }

    var b bytes.Buffer
    b.WriteString("{")
    for i := 0; i < v.NumField(); i++ {
        if i > 0 {
            b.WriteString(" ")
        }
        v2 := v.Field(i)
        if names {
            b.WriteString(t.Field(i).Name)
            b.WriteString(":")
        }
        if v2.CanInterface() {
            if st, ok := v2.Interface().(fmt.Stringer); ok {
                b.WriteString(st.String())
                continue
            }
        }
        fmt.Fprint(&b, v2)
    }
    b.WriteString("}")
    return b.String()
}

func (a A) String() string {
    return PrintStruct(a, true)
}

func main() {
    a := A{time.Now(), 2, "hi!"}
    fmt.Println(a)
    fmt.Println(PrintStruct(a, true))
    fmt.Println(PrintStruct(a, false))
    fmt.Println(PrintStruct("I'm not a struct", true))
}
Copy after login

Testing this code produces:

{{63393490800 0 0x206da0}}
2009-11-10 23:00:00 +0000 UTC
{T:2009-11-10 23:00:00 +0000 UTC I:2 unexported:hi!}
{T:2009-11-10 23:00:00 +0000 UTC I:2 unexported:hi!}
{2009-11-10 23:00:00 +0000 UTC 2 hi!}
I'm not a struct
Copy after login

The above is the detailed content of How can I easily print Go structs using their fields' String() methods without implementing String() for every struct?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template