A small project, making a command-line tool to quickly define a word and/or list definitions. Just a fun exercise in Go. dictionary.txt from https://github.com/sujithps/Dictionary/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
992B

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "bufio"
  7. )
  8. func main() {
  9. var userword string
  10. if len(os.Args) < 1 {
  11. reader := bufio.NewReader(os.Stdin)
  12. fmt.Println("Please enter the word you wish to define:\n")
  13. userword, _ = reader.ReadString('\n')
  14. } else {
  15. userword = os.Args[1]
  16. }
  17. file, err := os.Open("dictionary.txt")
  18. if err != nil {
  19. fmt.Println("Error! Dictionary file not opening!")
  20. }
  21. defer file.Close()
  22. scanner := bufio.NewScanner(file)
  23. itis := false
  24. for scanner.Scan() {
  25. //fmt.Println(scanner.Text())
  26. definition := strings.Split(scanner.Text(), " ")
  27. ifso, _ := CompareStrings(definition[0], userword)
  28. if ifso {
  29. fmt.Println(scanner.Text())
  30. itis = true
  31. }
  32. }
  33. if !(itis) {
  34. fmt.Println("No definition found")
  35. }
  36. if err := scanner.Err(); err != nil {
  37. fmt.Println(err)
  38. }
  39. }
  40. func CompareStrings(stringA, stringB string) (bool, error) {
  41. if strings.EqualFold(stringA, stringB) {
  42. return true, nil
  43. } else {
  44. return false, nil
  45. }
  46. }