Menu Close

Go – Repeat() function in strings package in go

Here, We will learn about Repeat() function in strings package in go.  Repeat() function is used to get a new string consisting of count copies of the string in go golang.

Function prototype:

func Repeat(str string, count int) string

Return value:

Repeat() function in strings package returns
a new string consisting of count copies of the
string str.
It gives error if count is negative.

Example with code:

package main

import (
  "fmt"
  "strings"
)

func main() {

  s := strings.Repeat("go", 2)
  fmt.Println(s)

  s = strings.Repeat("lang", 1)
  fmt.Println(s)

  s = strings.Repeat("HI", 4)
  fmt.Println(s)

  s = strings.Repeat("BI", 3)
  fmt.Println(s)
}

Output:

gogo
lang
HIHIHIHI BIBIBI

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, strings

Leave a Reply

Your email address will not be published. Required fields are marked *