[go-nuts] Is there +v Stringer interface?

2020-06-24 Thread yves baumes
When I want to override the '%v' format, I declare a String() method.
When I want to override the '%#v' format, I declare a GoString() method.

But there is nothing about '%+v'? There is no PlusStringer() method ?

Let's take the following exemple, the %v and %+v formatter prints the exact 
same results. Which is annoying somehow..  

```
package main

import ("fmt")

type page struct {
title string
body  []byte
}

func (p *page) String() string {
return fmt.Sprint("{ ", p.title, " ", string(p.body), " }")
}

func main() {
const title = "helloworld"
const text = "Hello everyone"
p := &page{title: title, body: []byte(text)}
log.Printf("p = %v", p) // prints p = { helloworld Hello everyone }
log.Printf("p = %+v", p) // prints p = { helloworld Hello everyone }
log.Printf("p = %#v", p) // prints p = &main.page{title:"helloworld", 
body:[]uint8{0x48, 0x65, 0x6c, [etc]
}

```

Any clue on how to adapt the String() method to print the fields names?



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2506ab0c-5558-4c72-8ad4-a8ad0006ea08o%40googlegroups.com.


Re: [go-nuts] Is there +v Stringer interface?

2020-06-25 Thread yves baumes
Thank you all :)

Le jeu. 25 juin 2020 à 06:37, andrey mirtchovski  a
écrit :

> >> fmt.Formatter is woefully under documented.
> >
> > My reference is pkg/errros -
> https://github.com/pkg/errors/blob/master/errors.go#L127
>
> we have wasted tens of man-hours hunting for a bug that didn't
> manifest in logs due to that custom formatter. %#v basically went
> directly to stringer without embedding type information (the
> fallthrough, line 135 in the link you linked). it was difficult to
> hunt and lead us to believe that custom formatters should be avoided.
> in the end we switched away from pkg/errors using the new stuff
> available as standard in go 1.14. the other lesson we learned is that
> it's better to be explicit than implicit. wrapping nil errors with
> .Wrap returning nil resulted in a lot of assumptions that confused
> coders not experienced with that package. when we switched to %w
> errors popped up where not expected because the explicitness of the %w
> flag doesn't allow a nil error to be used with it.
>
> nothing against the package, it served us well for a very long time.
> but it was time for a change.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAK4xykVQrA22MjKpMweR9QDmOsVEEg5TVEhHFqZKoVva7y8HAw%40mail.gmail.com
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAF1Qswgt-G0MZoxxgroUQ-k1frKX4gFKagG1f1nkZvW2tL9Tnw%40mail.gmail.com.


[go-nuts] Expanding variables and fmt.Printf() issue

2020-06-29 Thread yves baumes
Hello,

considering this code

```
package main

import (
"fmt"
)

func main() {
host := []string{"127", "0", "0", "1"}

fmt.Printf("%v.%v.%v.%v\n", host[0], host[1], host[2], host[3])
fmt.Printf("%v.%v.%v.%v\n", host[0:4]...)
}
```

The first Printf works and prints the host value.
While the second one does not compile : "./args.go:11:34: cannot use 
host[0:4] (type []string) as type []interface {} in argument to fmt.Printf"

If I use append() instead of Printf(), this expanding of the host variables 
just works out. Is this a compiler bug in the case of the fmt.Printf() ?

Regards
Yves.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5fe08421-eaa4-4285-bd46-ecdfc5403465o%40googlegroups.com.