What is the Go equivalent of PHP's 'implode'?
Go equivalent of PHP's 'implode'
stackoverflow.com/tags/go/info –
Responsiveness
en.wikipedia.org/wiki/Go_%28programming_language%29 –
Lukewarm
In the standard library: strings.Join
func Join(a []string, sep string) string
http://golang.org/pkg/strings/#Join
Cheers!
Thanks a lot! I spent about half an hour searching for this and stackoverflow got me the answer in less than 5 minutes! OTOH, I now feel a bit dumb to not have browsed through the "strings" package documentation. –
Responsiveness
Join in the strings library. It requires the input array to be strings only (since Go is strongly typed).
Here is an example from the manual:
s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
s := []string{"this", "is", "a", "joined", "string\n"};
strings.Join(s, " ");
Did this help you?
As i remember, PHP don't have strict typing. Probably not worst idea to use something like this.
package main
import (
"fmt"
"strings"
)
func Implode(glue string, args ...interface{}) string {
data := make([]string, len(args))
for i, s := range args {
data[i] = fmt.Sprint(s)
}
return strings.Join(data, glue)
}
type S struct {
z float64
}
func main() {
v := Implode(", ", 1, "2", "0.2", .1, S{});
fmt.Println(v)
}
© 2022 - 2024 — McMap. All rights reserved.