Welcome To Golang By Example

Create count/repeating copies of a string in Go (Golang)

Table of Contents

Overview

strings package of GO provides a Repeat method that can be used to create repeating copies of a given string. It takes input as the number of counts.

Below is the signature of the function. It returns a copy of the string

func Repeat(s string, count int) string

Let’s look at the working code

Code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    res := strings.Repeat("abc", 4)
    fmt.Println(res)
}

Output:

abcabcabcabc