The "%#v" print verb is intended to be used for unambiguous printing of a type, but that means it will have a prefix of `[]string` in order to ensure the type is unambiguous. I personally use %q a lot any time I know I have a string (or set of strings) which might be ambiguous in some way, but I don't want the `[]string` prefix in there. See: https://play.golang.org/p/70Ryd50IGt6
On Thursday, March 18, 2021 at 4:31:32 AM UTC-6 Brian Candler wrote: > Sure. I don't have a problem with nil slice and empty slice showing the > same, because in all important ways they behave the same - i.e. they have > len() of 0, you can append() to them, etc. The only behavioural difference > I can think of is if you explicitly test "foo == nil". > > However, a slice with len=1 that showed the same as one with len=0, was > what confused me :-) > > On reflection, %v is intentionally ambiguous. There are other examples, > e.g. > - []string{"a","b"} and []string{"a b"} > - []string{"", ""} and []string{" "} > > On Wednesday, 17 March 2021 at 21:24:46 UTC tapi...@gmail.com wrote: > >> Printing a nil slice also get the same output []. >> >> I remembered Rob Pike ever said in an issue thread that this can't be >> changed now for compatibility reason. >> >> On Monday, March 15, 2021 at 8:18:46 AM UTC-4 Brian Candler wrote: >> >>> I was slightly surprised to discover that the Print() output for an >>> empty slice, and a 1-element slice containing the empty string, are the >>> same: >>> >>> https://play.golang.org/p/btkzgk4LMT9 >>> >>> It does follow logically from the rules >>> <https://golang.org/pkg/fmt/#hdr-Printing>. I guess I need to train >>> myself to use %q or %#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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9e87e9bc-fbd9-4729-8721-689a62db8130n%40googlegroups.com.