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.
Syntax:
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.
Example:
// Go program to illustrate how to repeat // the elements of the slice of bytes package main import ( "bytes" "fmt") // Main function func main() { // Creating and initializing // the slice of bytes // Using shorthand declaration slice_1 := []byte{'G', 'E', 'E', 'K', 'S'} slice_2 := []byte{'A', 'P', 'P', 'L', 'E'} // Repeating the given slice // Using Repeat function res1 := bytes.Repeat(slice_1, 2) res2 := bytes.Repeat(slice_2, 4) res3 := bytes.Repeat([]byte("Geeks"), 5) // Display the results fmt.Printf("Result 1: %s", res1) fmt.Printf("\nResult 2: %s", res2) fmt.Printf("\nResult 3: %s", res3) } |
Output:
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.
Example:
// Go program to illustrate how to repeat // the elements of the slice of bytes package main import ( "bytes" "fmt") // Main function func main() { // Creating and initializing // the slice of bytes // Using shorthand declaration slice_1 := []byte{'G', 'E', 'E', 'K', 'S'} slice_2 := []byte{'A', 'P', 'P', 'L', 'E'} // Repeating the given slice // Using Repeat function // If we use a negative // value in the count // then this function will // panic because negative // values are not allowed to count res1 := bytes.Repeat(slice_1, -2) res2 := bytes.Repeat(slice_2, -4) res3 := bytes.Repeat([]byte("Geeks"), -5) // Display the results fmt.Printf("Result 1: %s", res1) fmt.Printf("\nResult 2: %s", res2) fmt.Printf("\nResult 3: %s", res3) } |
Output:
panic: bytes: negative Repeat count
goroutine 1 [running]:
bytes.Repeat(0x41a787, 0x5, 0x5, 0xfffffffe, 0x66ec0, 0x3f37, 0xf0a40, 0x40a0d0)
/usr/local/go/src/bytes/bytes.go:485 +0x1a0
main.main()
/tmp/sandbox192154574/prog.go:22 +0x80
Recommended Posts:
- How to split a slice of bytes in Golang?
- How to trim a slice of bytes in Golang?
- How to replace all the elements in slice of bytes in Golang?
- How to find the last index value in slice of bytes in Golang?
- How to find the first index value in slice of bytes in Golang?
- How to replace a specified element in slice of bytes in Golang?
- How to convert a slice of bytes in lowercase in Golang?
- How to split a slice of bytes after the given separator in Golang?
- How to convert a slice of bytes in uppercase in Golang?
- Golang | Splitting a slice of bytes after the specified separator
- How to trim suffix from the slice of bytes in Golang?
- How to trim prefix from the slice of bytes in Golang?
- Check if the Slice of bytes starts with specified prefix in Golang
- How to trim right-hand side of a slice of bytes in Golang?
- How to trim white spaces from the slice of bytes in Golang?
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.



