To repeat a string N times in Go, you can use the strings.Repeat
function from the strings
package.
In this example,
strings
package to use the Repeat
function.str
with a string value.n
that specifies the number of times the string should be repeated.strings.Repeat
function to repeat the string n
times and store the result in a new variable named result
.package main
import (
"fmt"
"strings"
)
func main() {
str := "GoLang "
n := 3
result := strings.Repeat(str, n)
fmt.Println("Repeated string is:", result)
}
Repeated string is: GoLang GoLang GoLang
In this example,
strings
package to use the Repeat
function.str
with a different string value.n
that specifies the number of times the string should be repeated.strings.Repeat
function to repeat the string n
times and store the result in a new variable named result
.package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello "
n := 5
result := strings.Repeat(str, n)
fmt.Println("Repeated string is:", result)
}
Repeated string is: Hello Hello Hello Hello Hello
In this tutorial, we learned How to Repeat a String N Times in Go language with well detailed examples.