Golang bytes.Join() function example

package bytes

Join concatenates the elements of input slice to create a new byte slice. A separator is placed between elements in the resulting slice.

Golang bytes.Join() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

  a := []byte("Hello")
  b := []byte(" World!")
  c := [][]byte{a, b}

  d := []byte{}

  d = bytes.Join(c, []byte(", "))
  fmt.Println(string(d))

  e := []byte{}

  e = bytes.Join(c, []byte("--"))
  fmt.Println(string(e))

 }

Output :

Hello, World!

Hello-- World!

Reference :

http://golang.org/pkg/bytes/#Join

Advertisement