Go的等价于PHP的’implode’函数

huangapple go评论98阅读模式
英文:

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

Joinstrings库中。它要求输入的数组只能是字符串(因为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)
}

huangapple
  • 本文由 发表于 2012年8月23日 17:40:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/12088839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定