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
+11k Golang : How to determine a prime number?
+4.9k Golang : Calculate a pip value and distance to target profit example
+6.8k Golang : Normalize email to prevent multiple signups example
+6.5k Golang : How to validate ISBN?
+15.5k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+9.9k Golang : Channels and buffered channels examples
+30.3k Golang : How to redirect to new page with net/http?
+17.2k Golang : How to tell if a file is compressed either gzip or zip ?
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+13.1k Golang : How to calculate the distance between two coordinates using Haversine formula
+35.7k Golang : Integer is between a range