英文:
Go equivalent of PHP's 'implode'
问题
Go中与PHP的'implode'函数相对应的是什么?
英文:
What is the Go equivalent of PHP's 'implode'?
答案1
得分: 70
在标准库中:strings.Join
func Join(a []string, sep string) string
http://golang.org/pkg/strings/#Join
Cheers!
英文:
In the standard library: strings.Join
func Join(a []string, sep string) string
http://golang.org/pkg/strings/#Join
Cheers!
答案2
得分: 13
Join在strings库中。它要求输入的数组只能是字符串(因为Go是强类型语言)。
这是手册中的一个例子:
s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
英文:
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, ", "))
答案3
得分: 7
s := []string{"this", "is", "a", "joined", "string\n"};
strings.Join(s, " ");
Did this help you?
英文:
s := []string{"this", "is", "a", "joined", "string\n"};
strings.Join(s, " ");
Did this help you?
答案4
得分: 4
根据您的要求,以下是翻译好的部分:
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)
}
英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论