Created
December 22, 2020 13:21
-
-
Save rodrwan/e56012c7406989ff207932a053bbff9b to your computer and use it in GitHub Desktop.
Función para concatenar un arreglo de ids usando bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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