Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created August 1, 2013 01:59
Show Gist options
  • Select an option

  • Save fsouza/6127850 to your computer and use it in GitHub Desktop.

Select an option

Save fsouza/6127850 to your computer and use it in GitHub Desktop.
exercício workshop

Crie um buscador de artigos por palavra. O programa recebe uma lista de arquivos e abre um shell interativo, onde é possível buscar por palavras nesses artigos. Ao digitar uma palavra, o usuário receberá a lista de artigos que contém aquela palavra.

Código do main:

func main() {
    store, err := search.NewIndex(os.Args[1:]...)
    if err != nil {
        log.Fatal(err)
    }
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanLines)
    fmt.Print("> ")
    for scanner.Scan() {
        articles, err := store.Search(scanner.Text())
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Printf("%s.\n", strings.Join(articles, ", "))
        }
        fmt.Print("> ")
    }
    fmt.Println()
}

Exemplo de uso:

% ./article-search articles/*
> java
No articles found.
> c++
articles/cguide_3.txt, articles/pcgpe10.txt.
> cpu
articles/optimize.txt, articles/pcgpe10.txt, articles/tpudoc1.txt.
> unix
articles/archives.txt, articles/bourne1.txt.
> ^D
%

Desafio: implemente os operadores & e |. Estes operadores significam and e or, respectivamente. Buscas por "cpu & memory" devem trazer todos documentos que têm ambas as palavras, enquanto buscas por "cpu | memory" devem trazer todos os artigos que têm uma das duas palavras.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment