How to join the elements of the byte slice in Golang?

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In the Go slice of bytes, you are allowed to join the elements of the byte slice with the help of Join() function. Or in other words, Join function is used to concatenate the elements of the slice and return a new slice of bytes which contain all these joined elements separated by the given separator. It is defined under the bytes package so, you have to import bytes package in your program for accessing Join function.

Syntax:

func Join(slice_1 [][]byte, sep []byte) []byte

Here, sep is the separator placed between the elements of the resulting slice. Let us discuss this concept with the help of the examples:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Go program to illustrate
// how to join a slice of bytes
package main
  
import (
    "bytes"
    "fmt"
)
  
// Main function
func main() {
  
    // Creating and initializing
    // slices of bytes
    // Using shorthand declaration
    name := [][]byte{[]byte("Sumit"), 
                     []byte("Kumar"), 
                     []byte("Singh")}
    sep := []byte("-")
  
    // displaying name of the student in parts
    fmt.Printf("First Name: %s", name[0])
    fmt.Printf("\nMiddle Name: %s", name[1])
    fmt.Printf("\nLast Name: %s", name[2])
  
    // Join the first, middle, and
    // last name of the student
    // Using Join function
    full_name := bytes.Join(name, sep)
  
    // Displaying the name of the student
    fmt.Printf("\n\nFull name of the student: %s", full_name)
  
}

chevron_right


Output:

First Name: Sumit
Middle Name: Kumar
Last Name: Singh

Full name of the student: Sumit-Kumar-Singh

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to
// join the slices of bytes
package main
  
import (
    "bytes"
    "fmt"
)
  
// Main function
func main() {
  
    // Creating and initializing slices of bytes
    // Using shorthand declaration
    slice_1 := [][]byte{[]byte("Geeks"), []byte("for"), []byte("Geeks")}
      
    slice_2 := [][]byte{[]byte("Hello"), []byte("My"),
        []byte("name"), []byte("is"), []byte("Bongo")}
  
    // Displaying slices
    fmt.Println("Slice(Before):")
    fmt.Printf("Slice 1: %s ", slice_1)
    fmt.Printf("\nSlice 2: %s", slice_2)
  
    // Joining the elements of the slice
    // Using Join function
    res1 := bytes.Join(slice_1, []byte(" , "))
    res2 := bytes.Join(slice_2, []byte(" * "))
    res3 := bytes.Join([][]byte{[]byte("Hey"), []byte("I"), 
              []byte("am"), []byte("Apple")}, []byte("+"))
  
    // Displaying results
    fmt.Println("\n\nSlice(after):")
    fmt.Printf("New Slice_1: %s ", res1)
    fmt.Printf("\nNew Slice_2: %s", res2)
    fmt.Printf("\nNew Slice_3: %s", res3)
  
}

chevron_right


Output:

Slice(Before):
Slice 1: [Geeks for Geeks] 
Slice 2: [Hello My name is Bongo]

Slice(after):
New Slice_1: Geeks , for , Geeks 
New Slice_2: Hello * My * name * is * Bongo
New Slice_3: Hey+I+am+Apple


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.