Golang : Check if a word is countable or not
Here is an inflector
function useful in determining if a given word is countable or not. The function returns TRUE if the word is countable or FALSE if not.
Here you go!
package main
import (
"fmt"
"strings"
)
func IsCountable(input string) bool {
// dictionary of word that has no plural version
toCheck := strings.ToLower(input)
var nonCountable = []string{
"audio",
"bison",
"chassis",
"compensation",
"coreopsis",
"data",
"deer",
"education",
"emoji",
"equipment",
"fish",
"furniture",
"gold",
"information",
"knowledge",
"love",
"rain",
"money",
"moose",
"nutrition",
"offspring",
"plankton",
"pokemon",
"police",
"rice",
"series",
"sheep",
"species",
"swine",
"traffic",
"wheat",
}
for _, v := range nonCountable {
if toCheck == v {
return false
}
}
return true
}
func main() {
fmt.Println("Traffic is countable? ", IsCountable("traffic"))
fmt.Println("Swine is countable? ", IsCountable("SwINE"))
fmt.Println("Fish is countable? ", IsCountable("FISH"))
fmt.Println("Apple is countable? ", IsCountable("Apple"))
fmt.Println("People is countable? ", IsCountable("People"))
}
Output:
Traffic is countable? false
Swine is countable? false
Fish is countable? false
Apple is countable? true
People is countable? true
See also : Golang : Convert word to its plural form example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+33.7k Golang : Strip slashes from string example
+12.8k Golang : How to check if your program is running in a terminal
+9.9k Golang : Read until certain character to break for loop
+12k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+17.8k Golang : How to make function callback or pass value from function as parameter?
+7.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+4.8k Golang : Use NLP to get sentences for each paragraph example
+15.2k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+31.2k Golang : Regular Expression for alphanumeric and underscore
+30.1k Golang : Math pow(the power of x^y) example
+5.2k Javascript : Generate random key with specific length
+11.4k Swift : Convert (cast) Int or int32 value to CGFloat