How to count specific characters present in the slice in Golang?
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 can count the elements present in it with the help of Count function. This function returns the total number of elements or the total number of some specified element available in the given slice. This function is defined under the bytes package so, you have to import bytes package in your program for accessing Count function.
func Count(slice_1, sep_slice byte) int
If the sep_slice is empty slice, then this function returns 1 + the number of UTF-8-encoded code points present in the slice_1.
Result 1: 4 Result 2: 2 Result 3: 2 Result 4: 6 Result 5: 9