Пример #1
0
func (this *UrbanDictionaryDatabase) Define(name string, query string) (definitions []*dictd.Definition) {
	response, err := api.DefineWord(query)
	if err != nil {
		log.Printf("Error getting from UD: %s", err)
		return
	}

	for _, el := range response.Results {
		definitions = append(definitions, &dictd.Definition{
			Word:             el.Word,
			Definition:       el.Definition,
			DictDatabase:     this,
			DictDatabaseName: name,
		})
		log.Printf("%s\n", el.Word)
	}
	return
}
Пример #2
0
func main() {
	var version bool

	// parse flags
	flag.BoolVar(&version, "version", false, "print version and exit")
	flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
	flag.Parse()

	args := os.Args

	if len(args) <= 1 || args[1] == "help" {
		fmt.Println(BANNER)
		return
	}

	if version || args[1] == "version" {
		fmt.Println(VERSION)
		return
	}

	word := args[1]

	response, err := api.DefineWord(word)

	if err != nil {
		fmt.Printf("Decoding api response as JSON failed: %v", err)
		return
	}

	defResponse := fmt.Sprintf("%d definitions returned\n", len(response.Results))

	for _, def := range response.Results {
		defResponse += fmt.Sprintf("\n%s\n--(%s) <%s>\n", def.Definition, def.Word, def.Link)
	}

	fmt.Println(defResponse)
}