Problem getting the right case in a switch statement

Trying to make a switch statement that outputs a case depending. Only the default case works, and i think unfortunately.
Also, is there anyway to improve the countVowel method? think it can look way better

You can use map type - Go maps in action

Thanks, found a different way

string.Count() returns a int which means the number of times a substring is found in the source string. so
numOfa := strings.Count(string(s), “a”) returns 1
I think it is easier use a map. For example:

package main

import (

type vowelMap map[string]int
type MyString string

//made a method on my MyString type for finding vowels
func (m MyString) findAndCountVowel() vowelMap {
	vm := make(vowelMap)
	vowels := "aeiou"

	for _, vowel := range m {
		if strings.ContainsRune(vowels, vowel) {
			ch := string(vowel)
			vm[ch] = vm[ch] + 1
	return vm

func main() {
	var s MyString = "Hey! Lets see how many vowels we got "

	//get the vowels in my string

	m := s.findAndCountVowel()
	fmt.Printf("Vowels are %v", m)
1 Like

That is right now. The use of switch was logically invalid. A case would have been executed only if the number of letters was equal to the ASCII code of the letter.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.