Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

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

Posted on March 22, 2023March 22, 2023 by admin

Table of Contents

  • Overview
  • Code:

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
  • go
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP