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:
// 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
Recommended Posts:
- How to count specific characters present in the slice in Golang?
- Check if the given characters is present in Golang String
- How to Replace Characters in Golang String?
- Program to find the Length of the longest substring without repeating characters in Golang
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Repeating a String for Specific Number of Times in Golang
- Counting number of repeating words in a Golang String
- string.Contains Function in Golang with Examples
- Converting a string variable into Boolean, Integer or Float type in Golang
- Golang Program that uses String Switch
- How to Trim a String in Golang?
- How to convert a string in uppercase in Golang?
- How to convert a string in lower case in Golang?
- How to Split a String in Golang?
- How to find the index value of specified string in Golang?
- How to find the last index value of specified string in Golang?
- Check if the String starts with specified prefix in Golang
- Check if the String ends with specified suffix in Golang
- How to check String for equality under Unicode case folding in Golang?
- Golang | How to find the index of rune in the string?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

