Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowelsvowel by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowels by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowel by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowels by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowels by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowels by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.
Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)
Handle errors. In Go,
foo, _ := something()
is a big code smell.Iterating on a string gives you a
rune
; don't convert it to a one-char string just to compare it to another one-char string. Compare it to anotherrune
instead ('a'
instead of"a"
).Your functions
isVowel
andisConsonant
are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.Option 1:
var ( vowels = map[rune]bool{ 'a': true, 'e': true, 'i': true, 'o': true, 'u': true, } consonants = map[rune]bool{ // ... } )
Option 2:
var ( vowels = toSet('a', 'e', 'i', 'o', 'u') consonants = toSet(/* ... */) ) func toSet(chars ...rune) map[rune]bool { m := make(map[rune]bool, len(chars)) for _, c := range chars { m[c] = true } return m }
You then check if a rune
r
is a vowels by just callingvowels[r]
.Your
s = fmt.Sprintf("%c", x)
statement looks a lot like printf debugging. Were you intending to keep it in your code?countVowels
andcountConsonants
can be inlined as well, and called on each char in the same loop like in Curtis' answer.Replace
foo += 1
byfoo++
.I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.