Re: [go-nuts] How to print arrays with commas and brackets

2020-08-07 Thread 'Dan Kortschak' via golang-nuts
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



-- 
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/42f2c00adc67d6100724122d93e8467513ecee12.camel%40kortschak.io.


[go-nuts] How to print arrays with commas and brackets

2019-10-10 Thread Michele Caci
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!

-- 
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/c67ad055-0175-4981-b6e9-69233a954da6%40googlegroups.com.


Re: [go-nuts] How to print arrays with commas and brackets

2019-10-09 Thread Lutz Horn
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

--
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/6939742d3ca8ea4240b700fdbf64b73f%40posteo.de.


[go-nuts] How to print arrays with commas and brackets

2019-10-09 Thread Nalin Pushpakumara
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

-- 
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/f57d03aa-f1f1-4023-97b2-d4f0706e9f27%40googlegroups.com.