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
+14.4k Golang : invalid character ',' looking for beginning of value
+15.7k Golang : Capture stdout of a child process and act according to the result
+7.5k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+6.7k Golang : Generate human readable password
+7.8k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+7.2k Prevent Write failed: Broken pipe problem during ssh session with screen command
+24.7k Golang : missing Mercurial command
+5.4k PHP : Proper way to get UTF-8 character or string length
+12.2k Golang : Generate Code128 barcode
+15k Golang : convert string or integer to big.Int type
+19.9k Golang : Sort and reverse sort a slice of strings
+18.9k nginx: [emerg] unknown directive "passenger_enabled"