How to Count the Number of Repeated Characters in a Golang String? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 3 Likes 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. In Go string, you can count some specific Unicode code points or the number of non-overlapping instances of costr (Repeated Characters) in the string with the help of Count() function. This function returns a value which represents the total number of the given string or Unicode code points present in the string. It is defined under the strings package so, you have to import strings package in your program for accessing Count function. Syntax: func Count(str, costr string) int Here, str is the original string and costr is a string which we want to count. If the value of costr is empty, then this function returns 1 + the number of Unicode code points in str. Example: C // Go program to illustrate how to // count the elements of the string package main import ( "fmt" "strings" ) // Main function func main() { // Creating and initializing the strings str1 := "Welcome to the online portal of GeeksforGeeks" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // Displaying strings fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // Counting the elements of the strings // Using Count() function res1 := strings.Count(str1, "o") res2 := strings.Count(str2, "do") // Here, it also counts white spaces res3 := strings.Count(str3, "") res4 := strings.Count("GeeksforGeeks, geeks", "as") // Displaying the result fmt.Println("\nResult 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) } Output: String 1: Welcome to the online portal of GeeksforGeeks String 2: My dog name is Dollar String 3: I like to play Ludo Result 1: 6 Result 2: 1 Result 3: 20 Result 4: 0 Create Quiz Comment A ankita_saini Follow 3 Improve A ankita_saini Follow 3 Improve Article Tags : Go Language Golang Golang-String Explore OverviewGo Programming Language (Introduction) 7 min read How to Install Go on Windows? 3 min read How to Install Golang on MacOS? 4 min read Hello World in Golang 3 min read FundamentalsIdentifiers in Go Language 3 min read Go Keywords 2 min read Data Types in Go 7 min read Go Variables 9 min read Constants- Go Language 6 min read Go Operators 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if) 5 min read Loops in Go Language 5 min read Switch Statement in Go 2 min read Functions & MethodsFunctions in Go Language 3 min read Variadic Functions in Go 3 min read Anonymous function in Go Language 2 min read main and init function in Golang 2 min read What is Blank Identifier(underscore) in Golang? 3 min read Defer Keyword in Golang 3 min read Methods in Golang 3 min read StructureStructures in Golang 7 min read Nested Structure in Golang 3 min read Anonymous Structure and Field in Golang 3 min read ArraysArrays in Go 7 min read How to Copy an Array into Another Array in Golang? 3 min read How to pass an Array to a Function in Golang? 2 min read SlicesSlices in Golang 14 min read Slice Composite Literal in Go 3 min read How to sort a slice of ints in Golang? 2 min read How to trim a slice of bytes in Golang? 3 min read How to split a slice of bytes in Golang? 3 min read StringsStrings in Golang 7 min read How to Trim a String in Golang? 2 min read How to Split a String in Golang? 3 min read Different ways to compare Strings in Golang 2 min read PointersPointers in Golang 8 min read Passing Pointers to a Function in Go 3 min read Pointer to a Struct in Golang 3 min read Go Pointer to Pointer (Double Pointer) 4 min read Comparing Pointers in Golang 3 min read ConcurrencyGoroutines - Concurrency in Golang 2 min read Select Statement in Go Language 4 min read Multiple Goroutines 2 min read Channel in Golang 7 min read Unidirectional Channel in Golang 2 min read Like