What is the Go equivalent of PHP's 'implode'?
The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.
PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array. PHP implode and PHP explode are two common functions used in PHP when working with arrays and strings in PHP.
join appends the delimiter to the last element while implode doesn't.
The first parameter is 'separator' The separator is the optional parameter, and it is there to specify what is needed to be put between the array components. By default, it appears as ”, “ which denotes an empty string. The array values are joined to form a string and are separated by the separator parameter.
In the standard library: strings.Join
func Join(a []string, sep string) string
http://golang.org/pkg/strings/#Join
Cheers!
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, ", "))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With