How to print arrays with commas and brackets

4,210 views
Skip to first unread message

Nalin Pushpakumara

unread,
Oct 9, 2019, 10:25:40 AM10/9/19
to golang-nuts
Hi,
I tried to print array with brackets and commas in golang. I want to get array like this.
["record1", "record2". "record3"]

Does anyone know how to do it?

Thank you

Lutz Horn

unread,
Oct 9, 2019, 10:38:32 AM10/9/19
to golang-nuts
> I tried to print array with brackets and commas in golang. I want to
> get
> array like this.
> ["record1", "record2". "record3"]

Encode the array to JSON:

```
package main

import (
"encoding/json"
"fmt"
"log"
"os"
)

func main() {
strings := [3]string{"foo", "bar", "baz"}
stringsJson, err := json.Marshal(strings)
if err != nil {
log.Fatal("Cannot encode to JSON ", err)
}
fmt.Fprintf(os.Stdout, "%s", stringsJson)
}
```

Output:

["foo","bar","baz"]

Lutz

Michele Caci

unread,
Oct 10, 2019, 1:11:59 PM10/10/19
to golang-nuts
Hello Nalin,

If you use a slice of strings to hold your data, as an alternative you could go with

fmt.Printf("[%s]", strings.Join(your_slice_of_strings, ","))

Cheers!

ranjan1...@gmail.com

unread,
Aug 6, 2020, 12:24:38 PM8/6/20
to golang-nuts
Hello Michele

This won't print in the array format rather it would print it in other way.
stringArray := []string{"Hello", "world", "!"}
        fmt.Printf("[%s]", strings.Join(stringArray , ","))

Output [Hello,world,!]

Raffaele Sena

unread,
Aug 6, 2020, 12:48:09 PM8/6/20
to ranjan1...@gmail.com, golang-nuts
One option is to use json.Marshal or json.Encoder.Encode

fmt.Printf("%q", []string{"Hello", "world", "!") would quote the separate strings and put the square brackets, but doesn't comma separate the items.


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/10afc2a5-bbc9-4a21-ab14-112b1cd3e74co%40googlegroups.com.

Henry

unread,
Aug 7, 2020, 10:45:44 PM8/7/20
to golang-nuts
Or you can do it manually:

func print(array []string) string {
var buffer strings.Builder
buffer.WriteString("[")
for index, item := range array {
if index != 0 {
buffer.WriteString(", ")
}
buffer.WriteString("\"" + item + "\"")
}
buffer.WriteString("]")
return buffer.String()
}
Message has been deleted

Kurtis Rader

unread,
Aug 7, 2020, 10:53:09 PM8/7/20
to Henry, golang-nuts
On Fri, Aug 7, 2020 at 7:46 PM Henry <henry.ad...@gmail.com> wrote:
Or you can do it manually:

func print(array []string) string {
var buffer strings.Builder
buffer.WriteString("[")
for index, item := range array {
if index != 0 {
buffer.WriteString(", ")
}
buffer.WriteString("\"" + item + "\"")
}
buffer.WriteString("]")
return buffer.String()
}

Consider what is emitted if the item contains a double-quote rune. In other words, your solution is naive and only produces unambiguous, correct, output for a subset of cases.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Dan Kortschak

unread,
Aug 7, 2020, 11:46:07 PM8/7/20
to golan...@googlegroups.com
On Wed, 2019-10-09 at 06:02 -0700, Nalin Pushpakumara wrote:
> Hi,
> I tried to print array with brackets and commas in golang. I want to
> get
> array like this.
> ["record1", "record2". "record3"]
>
> Does anyone know how to do it?
>
> Thank you
>

Not the most efficient, but simple and clear.

func printSlice(a []string) string {
q := make([]string, len(a))
for i, s := range a {
q[i] = fmt.Sprintf("%q", s)
}
return fmt.Sprintf("[%s]", strings.Join(q, ", "))
}

https://play.golang.org/p/cn9EaL0x-2v



Henry

unread,
Aug 8, 2020, 12:23:21 AM8/8/20
to golang-nuts
First, there is no such requirement in the OP's original post and there is no mention whether the output is going to be used for another processing that requires character escaping. The correctness of a solution is judged against its requirements.

Second, judging from the OP's question, it appears that the OP is relatively new to Go or perhaps the field of programming in general and he/she is looking for a simple algorithm to guide him/her through. If you were to offer a sophisticated solution, you are not helping the OP. 

Finally, it should be relatively easy for the OP to build upon the given solution and tailor it to his/her needs, and whether the OP would like to escape certain characters or format it in any other way.

On Saturday, August 8, 2020 at 9:53:09 AM UTC+7, Kurtis Rader wrote:
Reply all
Reply to author
Forward
0 new messages