FAQ
Is there a better, built in method to print a structs values without
defining each variable within it?

type Object struct {
   X, Y, Z float32
   foo, bar string
}

func main() {
     foo := Object{1, 2, 3.14, "foo bar", "bar"}
     fmt.Println(foo)
}

//{1 2 3.14 foo bar bar}

I think a better way to print this would be line delim rather than space,
or perhaps enclose strings? I'd rather not do a custom printf with each
value inside the struct if possible.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Raffaele Sena at Nov 4, 2013 at 6:10 pm
    fmt.Printf("%#v\n", foo)

    //main.Object{X:1, Y:2, Z:3.14, foo:"foo bar", bar:"bar"}

    If you want newlines you can use my package "github.com/gobs/pretty"
    or serialize to Json (indented)
    On Mon, Nov 4, 2013 at 10:03 AM, Kyle Wolfe wrote:
    Is there a better, built in method to print a structs values without
    defining each variable within it?

    type Object struct {
    X, Y, Z float32
    foo, bar string
    }

    func main() {
    foo := Object{1, 2, 3.14, "foo bar", "bar"}
    fmt.Println(foo)
    }

    //{1 2 3.14 foo bar bar}

    I think a better way to print this would be line delim rather than space, or
    perhaps enclose strings? I'd rather not do a custom printf with each value
    inside the struct if possible.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Wolfe at Nov 4, 2013 at 6:17 pm
    Thank you!
    On Monday, November 4, 2013 1:10:24 PM UTC-5, Raffaele Sena wrote:

    fmt.Printf("%#v\n", foo)

    //main.Object{X:1, Y:2, Z:3.14, foo:"foo bar", bar:"bar"}

    If you want newlines you can use my package "github.com/gobs/pretty"
    or serialize to Json (indented)
    On Mon, Nov 4, 2013 at 10:03 AM, Kyle Wolfe wrote:
    Is there a better, built in method to print a structs values without
    defining each variable within it?

    type Object struct {
    X, Y, Z float32
    foo, bar string
    }

    func main() {
    foo := Object{1, 2, 3.14, "foo bar", "bar"}
    fmt.Println(foo)
    }

    //{1 2 3.14 foo bar bar}

    I think a better way to print this would be line delim rather than space, or
    perhaps enclose strings? I'd rather not do a custom printf with each value
    inside the struct if possible.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Nov 4, 2013 at 7:26 pm
    Study the fmt package documentation in detail, and you'll answer a lot of
    other questions you haven't had yet
    On Monday, November 4, 2013 11:03:04 AM UTC-7, Kyle Wolfe wrote:

    Is there a better, built in method to print a structs values without
    defining each variable within it?

    type Object struct {
    X, Y, Z float32
    foo, bar string
    }

    func main() {
    foo := Object{1, 2, 3.14, "foo bar", "bar"}
    fmt.Println(foo)
    }

    //{1 2 3.14 foo bar bar}

    I think a better way to print this would be line delim rather than space,
    or perhaps enclose strings? I'd rather not do a custom printf with each
    value inside the struct if possible.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 4, '13 at 6:03p
activeNov 4, '13 at 7:26p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase