How to repeat 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 are allowed to repeat the elements of the slice to a specific number of times with the help of the Repeat() function. This method returns a new string which contains the repeated elements of the slice. It is defined under the bytes package so, you have to import bytes package in your program for accessing Repeat function.
func Repeat(slice_1 byte, count int) byte
Here, slice_1 represents the slice of bytes and the count value represents how many times you want to repeat the elements of the given slice_1.
Result 1: GEEKSGEEKS Result 2: APPLEAPPLEAPPLEAPPLE Result 3: GeeksGeeksGeeksGeeksGeeks
Note: This method will panic if the value of the count is negative or the result of (len(slice_1) * count) overflows.
panic: bytes: negative Repeat count
goroutine 1 [running]:
bytes.Repeat(0x41a787, 0x5, 0x5, 0xfffffffe, 0x66ec0, 0x3f37, 0xf0a40, 0x40a0d0)