Menu Close

Go – Join() fucntion in strings package in go

Here, We will learn about Join() fucntion in strings package in go.  Join() function is used to concatenates the elements of its argument to create a single string in go golang.

Function prototype:

func Join(strs []string, sep string) string

Return value:

Join() function in strings package returns
a string after concatenating the elements of
its first argument.
The separator string sep is placed between
elements in the resulting string.

Example with code:

package main

import (
  "fmt"
  "strings"
)

func main() {
  s := []string{"foo", "bar", "baz"}
  fmt.Println(strings.Join(s, ", "))

  s = []string{"foo", "bar", "baz"}
  fmt.Println(strings.Join(s, "@"))

  s = []string{"foo", "bar", "baz"}
  fmt.Println(strings.Join(s, ""))

  s = []string{"foo", "bar", "baz"}
  fmt.Println(strings.Join(s, " "))
}

Output:

foo, bar, baz
foo@bar@baz
foobarbaz
foo bar baz

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, strings

Leave a Reply

Your email address will not be published. Required fields are marked *