Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Repeating a String for Specific Number of Times in Golang
Next article icon

Repeating a String for Specific Number of Times in Golang

Last Updated : 15 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 !..

Next Article
Repeating a String for Specific Number of Times in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang
  • Golang-String

Similar Reads

    Counting number of repeating words in a Golang String
    Given a string, the task is to count the number of words being repeated in that particular string in Golang. Example: Input: s = "She is mother of my mother." Output: She = 1 is = 1 mother = 2 of = 1 my = 1 To count the number of repeating words, first, the string is taken as input and then strings.
    2 min read
    Searching an element of string type in Golang slice
    In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search
    3 min read
    Golang | Splitting a slice of bytes after the specified separator
    In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar
    3 min read
    How to Get the String in Specified Base in Golang?
    Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
    2 min read
    How to Split a String in Golang?
    In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences