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
+20.1k Golang : Count number of digits from given integer value
+21.8k Golang : How to reverse slice or array elements order
+4.4k Linux/MacOSX : Search and delete files by extension
+7.5k Golang : How to handle file size larger than available memory panic issue
+5.7k List of Golang XML tutorials
+9k Golang : Get SPF and DMARC from email headers to fight spam
+16.5k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+39.6k Golang : Remove dashes(or any character) from string
+10.1k Golang : Edge detection with Sobel method
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate