How to join the elements of the byte slice in Golang?
Last Updated :
26 Aug, 2019
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:
C
// 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:
C
// 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
Similar Reads
How to split a slice of bytes in Golang? In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
3 min read
How to compare two slices of bytes in Golang? In Go, a slice is a powerful, flexible data structure that allows elements of the same type to be stored in a variable-length sequence. When working with byte slices ([]byte), Go provides easy-to-use functions in the bytes package to compare them. In this article we will learn "How to compare two sl
2 min read
How to trim a slice of bytes 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 ar
3 min read
How to check equality of slices of bytes in Golang? In Go language slice is more powerful, flexible, and convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence that stores elements of a similar type, you are not allowed to store different types of elements in the same slice. In the Go slice of byes, you
3 min read
How to find the last index value in slice of bytes in Golang? In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. The LastIndex() function from the bytes package returns the last index of a specified value in a byte slice, or -1 if the value is not found.Examplepackage mainimport ( "bytes" "fmt")func
3 min read