Skip to content
Related Articles

Related Articles

How to join the elements of the byte slice in Golang?
  • Last Updated : 26 Aug, 2019
GeeksforGeeks - Summer Carnival Banner

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:




// 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)
  
}

Output:



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

Full name of the student: Sumit-Kumar-Singh

Example 2:




// 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)
  
}

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :