Last active
December 26, 2020 21:25
-
-
Save rodrwan/2983b0b74ed540c21f6df48eb146aed6 to your computer and use it in GitHub Desktop.
Función para concatenar un arreglo de ids con strings
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 ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
ids := []string{"id 1", "id 2", "id 3", "id 4", "id 5"} | |
// output: [{ id: id 1 },{ id: id 2 },{ id: id 3 },{ id: id 4 },{ id: id 5 }] | |
fmt.Println(concatWithStrings(ids)) | |
} | |
func concatWithStrings(ids []string) string { | |
first := fmt.Sprintf("[{ id: %v },", ids[0]) | |
middle := []string{} | |
for _, value := range ids[1 : len(ids)-1] { | |
middle = append(middle, fmt.Sprintf("{ id: %v }", value)) | |
} | |
sentence := strings.Join(middle, ",") | |
last := fmt.Sprintf(",{ id: %v }]", ids[len(ids)-1]) | |
return first + sentence + last | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment