FAQ
Per a previous question about passing an array to `fmt.Printf`
(https://groups.google.com/forum/#!topic/golang-nuts/yTxmAjoc_vw) I learned
that one has to convert it to an []interface{} first. Then I was reading
the FAQ, specifically http://golang.org/doc/faq#convert_slice_of_interface,
which presents:

     t := []int{1, 2, 3, 4}
     s := make([]interface{}, len(t))
     for i, v := range t {
         s[i] = v
     }


Which made we wonder. Why not just have a built in function for this, akin
it len(), that can handle it for any type. e.g. perhaps call if `face`:

       fruits = ["apples", "oranges", "pears"]
       fmt.Printf("%s %s %s", face(fruits))

That would at least help mitigate some the generics issues.


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

  • TR NS at Aug 8, 2013 at 4:16 am

    On Wednesday, August 7, 2013 9:13:22 PM UTC-4, TR NS wrote:

    fruits = ["apples", "oranges", "pears"]
    fmt.Printf("%s %s %s", face(fruits))

    Note that should be:

         fmt.Printf("%s %s %s", face(fruits)...)

    --
    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.
  • Gerard at Aug 8, 2013 at 4:44 am
    Please keep in mind that Go is not Python and take a look at the chapter
    "Learning Go" on http://golang.org/doc/

    Hope it helps.

    Gerard
    On Thursday, August 8, 2013 6:15:57 AM UTC+2, TR NS wrote:


    On Wednesday, August 7, 2013 9:13:22 PM UTC-4, TR NS wrote:


    fruits = ["apples", "oranges", "pears"]
    fmt.Printf("%s %s %s", face(fruits))

    Note that should be:

    fmt.Printf("%s %s %s", face(fruits)...)
    --
    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.
  • Volker Dobler at Aug 8, 2013 at 6:24 am

    Am Donnerstag, 8. August 2013 03:13:22 UTC+2 schrieb TR NS:

    Per a previous question about passing an array to `fmt.Printf` (
    https://groups.google.com/forum/#!topic/golang-nuts/yTxmAjoc_vw) I
    learned that one has to convert it to an []interface{} first. Then I was
    reading the FAQ, specifically
    http://golang.org/doc/faq#convert_slice_of_interface, which presents:

    t := []int{1, 2, 3, 4}
    s := make([]interface{}, len(t))
    for i, v := range t {
    s[i] = v
    }


    Which made we wonder. Why not just have a built in function for this, akin
    it len(), that can handle it for any type. e.g. perhaps call if `face`:
      Honest answer: Because this is almost never done. Really.
    Scan any part of Go's standard library or sub repos and I doubt
    you will find this more than a handful of times (if at all).

    V.

    --
    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.
  • TR NS at Aug 9, 2013 at 2:04 am

    On Thursday, August 8, 2013 2:24:33 AM UTC-4, Volker Dobler wrote:
    Am Donnerstag, 8. August 2013 03:13:22 UTC+2 schrieb TR NS:

    Honest answer: Because this is almost never done. Really.
    Scan any part of Go's standard library or sub repos and I doubt
    you will find this more than a handful of times (if at all).
    Its not something I would expect in the standard library, its a need of the
    end user. I find it hard to believe that it is totally uncommon to want to
    pass an array of string as the argument to a Printf. And that's just one
    particular use case. If it were so unheard of, why did it get it's own
    entry in the Go FAQ?


    --
    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 Aug 9, 2013 at 6:10 pm
    What they're getting at is that typical applications that must call
    fmt.Print* use []interface{} as their primary slice type, filling that from
    the start rather than copying a []string ad-hoc into an []interface{}
    whenever it must be printed. For debugging purposes (especially temporary
    debugging), the brackets around passing a slice directly into fmt.Println (*
    without* using ...) is not only something that most programmers are happy
    to live with, but can actually be quite useful.

    We're not saying that the need to print data without wrapping brackets is
    rare -- the way you want to do it is just rare in practice.
    On Thursday, August 8, 2013 8:04:23 PM UTC-6, TR NS wrote:


    On Thursday, August 8, 2013 2:24:33 AM UTC-4, Volker Dobler wrote:

    Am Donnerstag, 8. August 2013 03:13:22 UTC+2 schrieb TR NS:

    Honest answer: Because this is almost never done. Really.
    Scan any part of Go's standard library or sub repos and I doubt
    you will find this more than a handful of times (if at all).
    Its not something I would expect in the standard library, its a need of
    the end user. I find it hard to believe that it is totally uncommon to want
    to pass an array of string as the argument to a Printf. And that's just one
    particular use case. If it were so unheard of, why did it get it's own
    entry in the Go FAQ?

    --
    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.
  • Atomly at Aug 9, 2013 at 7:49 pm
    People convert back and forth between slices/arrays of a concrete type and
    []interface{} fairly often, I would say, and it leads to ugliness and/or
    inefficiency in some key codepaths, e.g. database Scanner, json Unmarshal,
    etc...

    :: atomly ::

    [ atomly@atomly.com : www.atomly.com : http://blog.atomly.com/ ...
    [ atomiq records : new york city : +1.347.692.8661 ...
    [ e-mail atomly-news-subscribe@atomly.com for atomly info and updates ...

    On Thu, Aug 8, 2013 at 2:24 AM, Volker Dobler wrote:

    Am Donnerstag, 8. August 2013 03:13:22 UTC+2 schrieb TR NS:

    Per a previous question about passing an array to `fmt.Printf` (
    https://groups.google.com/**forum/#!topic/golang-nuts/**yTxmAjoc_vw<https://groups.google.com/forum/#!topic/golang-nuts/yTxmAjoc_vw>)
    I learned that one has to convert it to an []interface{} first. Then I was
    reading the FAQ, specifically http://golang.org/doc/faq#**
    convert_slice_of_interface<http://golang.org/doc/faq#convert_slice_of_interface>,
    which presents:

    t := []int{1, 2, 3, 4}
    s := make([]interface{}, len(t))
    for i, v := range t {
    s[i] = v
    }


    Which made we wonder. Why not just have a built in function for this,
    akin it len(), that can handle it for any type. e.g. perhaps call if `face`:
    Honest answer: Because this is almost never done. Really.
    Scan any part of Go's standard library or sub repos and I doubt
    you will find this more than a handful of times (if at all).

    V.

    --
    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.
  • Kevin Gillette at Aug 11, 2013 at 3:22 pm
    That might be indicative of misuse, or misdesign of the packages; encoding/json and most database packages I've seen, sql or no, use reflect to adapt the data to and from the desired runtime type. This is one of the features of go that non-reflective or dynamically typed languages often can't deal with effectively, requiring code generators, black magic, or bug-susceptible verbose direct manipulation and conversion code.

    --
    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.
  • Carlos Castillo at Aug 9, 2013 at 5:06 am
    fmt.Printf("%s %s %s", fruits[0], fruits[1], fruits[2])

    If you have more or less elements in fruits, then you will get bad output.
    Therefore you would need to change format strings with each array length
    anyway. If you want a solution to write out arrays of strings in a format
    of your choice, as opposed to Printf's chosen format, you will need to
    write your own code. Programmers shouldn't be afraid of writing code.
    On Wednesday, August 7, 2013 6:13:22 PM UTC-7, TR NS wrote:

    Per a previous question about passing an array to `fmt.Printf` (
    https://groups.google.com/forum/#!topic/golang-nuts/yTxmAjoc_vw) I
    learned that one has to convert it to an []interface{} first. Then I was
    reading the FAQ, specifically
    http://golang.org/doc/faq#convert_slice_of_interface, which presents:

    t := []int{1, 2, 3, 4}
    s := make([]interface{}, len(t))
    for i, v := range t {
    s[i] = v
    }


    Which made we wonder. Why not just have a built in function for this, akin
    it len(), that can handle it for any type. e.g. perhaps call if `face`:

    fruits = ["apples", "oranges", "pears"]
    fmt.Printf("%s %s %s", face(fruits))

    That would at least help mitigate some the generics issues.

    --
    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
postedAug 8, '13 at 1:13a
activeAug 11, '13 at 3:22p
posts9
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase