Skip to content
Related Articles

Related Articles

How to repeat a slice of bytes 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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :