Skip to content
Related Articles

Related Articles

Repeating a String for Specific Number of Times in Golang

Improve Article
Save Article
  • Last Updated : 15 Nov, 2022
Improve Article
Save Article

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. 

You are allowed to repeat a string of for a specific number of times with the help of the Repeat() Function. This method returns a new string which contains a repeated string and it is defined under the strings package. So, you have to import strings package in your program for accessing Repeat function. 

Syntax:

func Repeat(str string, count int) string

Here, str represents the string that you want to repeat and the count value represents how many times you want to repeat str string. 

Example 1: 

Go




// Go program to illustrate how to repeat
// a string to a specific number of times
package main
 
import (
    "fmt"
    "strings"
)
 
// Main method
func main() {
 
    // Creating and initializing a string
    // Using shorthand declaration
    str1 := "Welcome to GeeksforGeeks !.."
    str2 := "This is the tutorial of Go"
 
    // Repeating the given strings
    // Using Repeat function
    res1 := strings.Repeat(str1, 4)
    res2 := str2 + strings.Repeat("Language..", 2)
 
    // Display the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

Result 1: Welcome to GeeksforGeeks !..Welcome to GeeksforGeeks !..Welcome to GeeksforGeeks !..Welcome to GeeksforGeeks !.. Result 2: This is the tutorial of GoLanguage..Language..

Note: This method will panics if the value of the count is negative or the result of (len(str) * count) overflows. 

Example 2: 

Go




// Go program to illustrate how to repeat
// a string to a specific number of times
package main
 
import (
    "fmt"
    "strings"
)
 
// Main method
func main() {
 
    // Creating and initializing a string
    // Using shorthand declaration
    str1 := "Welcome to GeeksforGeeks !.."
    str2 := "This is the tutorial of Go"
 
    // Repeating the given strings
    // Using Repeat function
    res1 := strings.Repeat(str1, 4)
 
    // If we use a negative value in the count
    // then this method will panic because negative
    // values are not allowed to count
    res2 := str2 + strings.Repeat("Language..", -2)
 
    // Display the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

panic: strings: negative Repeat count goroutine 1 [running]: strings.Repeat(0x104b22, 0xa, 0xfffffffe, 0x0, 0x450000, 0x70) /usr/local/go/src/strings/strings.go:533 +0x540 main.main() /tmp/sandbox829702598/prog.go:25 +0x80

Example 3: Using for loop

Go




// Go program to illustrate how to repeat
// a string to a specific number of times
package main
 
import (
    "fmt"
)
 
// Main method
func main() {
 
    // Creating and initializing a string
    // Using shorthand declaration
    str1 := "Welcome to GeeksforGeeks !.."
 
    // Repeating the given strings
    i := 0
    res1 := " "
    for i < 3 {
        res1 += str1
        i += 1
    }
 
    // Display the results
    fmt.Println("Result 1: ", res1)
 
}


Output

Result 1:   Welcome to GeeksforGeeks !..Welcome to GeeksforGeeks !..Welcome to GeeksforGeeks !..

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!