Skip to content

Instantly share code, notes, and snippets.

@rodrwan
Created December 22, 2020 13:21
Show Gist options
  • Save rodrwan/e56012c7406989ff207932a053bbff9b to your computer and use it in GitHub Desktop.
Save rodrwan/e56012c7406989ff207932a053bbff9b to your computer and use it in GitHub Desktop.
Función para concatenar un arreglo de ids usando bytes
package main
import (
"bytes"
"fmt"
)
func main() {
ids := []string{"id 1", "id 2", "id 3", "id 4", "id 5"}
fmt.Println(concatWithBuffer(ids))
}
func concatWithBuffer(ids []string) string {
sentence := bytes.NewBufferString(fmt.Sprintf("[{ id: %s },", ids[0]))
for _, value := range ids[1 : len(ids)-1] {
sentence.WriteString(fmt.Sprintf("{ id: %s },", value))
}
sentence.WriteString(fmt.Sprintf("{ id: %s }]", ids[len(ids)-1]))
return sentence.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment