https://play.golang.org/p/4IlgcBIbxk
I think displayed output is wrong!
https://play.golang.org/p/4IlgcBIbxk
I think displayed output is wrong!
Hey @sunheretic13, what exactly do you think is incorrect about the output?
If it is that it is displaying your array with brackets surrounding it, this is because you used %v
which displays arrays and slices in brackets.
You probably also want to be using a regular slice by using []string
instead of using [...]string
since then you can call your print information like this:
fmt.Printf("%v\n", strings.Join(arr, " "))
which will display all values joined as a string by spaces if that’s what you were expecting.
You will probably hardly ever need to use an actual array like you have used as opposed to using a regular slice.
Edit: Here’s an example of what I described above: https://play.golang.org/p/VpeCZNriOU.
Problem with native output %v
[Hello world and Hello world]
What do I see?
Brakets! Ok. Its array or slice
words - Ok. Its array/slice of strings
How many strings? I think 5. but in real - 4.
Something wrong in output, This is misleading.
It is a natural human perception, I would expect to see
[ “Hello” “world” “and” “Hello world”] or
[ “Hello”, “world”, “and”, “Hello world”]
I think it must be corrected in newest go releases
Javascript example:
a=[ “Hello”, “World”, “and”, “Hello World” ]
console.log(a);
this display:
[“Hello”, “World”, “and”, “Hello World”]
It’s nornal. not ambigous
Printf with %v
prints strings without quotes - hence also inside slices and arrays. See however the %#v
format to get closer to what you want.
Hmmm. Interesting…
More flexible that I think.
Thanks to calmh.
Topic closed (I thought go not supported it)
Try the %q
verb.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.