Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tradução dos exemplos "The Go Programming Language" #1

Open
claudioandre-br opened this issue Mar 30, 2017 · 1 comment
Open

Tradução dos exemplos "The Go Programming Language" #1

claudioandre-br opened this issue Mar 30, 2017 · 1 comment

Comments

@claudioandre-br
Copy link

Este é o repositório em que as traduções deverão ocorrer?


Abaixo anexo um trabalho "concluído" que seria submetido em um PR para revisão.

// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/

// See page 10.
//!+

//Traduzido pela equipe do GoPlex
//Mais em: https://github.com/ramalho/goplex

// Dup2 imprime e conta as linhas de texto duplicadas presentes em 
// um ou mais arquivos de entrada. O programa lê da entrada padrão
// (stdin) ou de uma lista de arquivos informados como parâmetro.
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	contagens := make(map[string]int)
	arquivos := os.Args[1:]
	if len(arquivos) == 0 {
		contarLinhas(os.Stdin, contagens)
	} else {
		for _, nomeArquivo := range arquivos {
			arquivo, err := os.Open(nomeArquivo)
			if err != nil {
				fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
				continue
			}
			contarLinhas(arquivo, contagens)
			arquivo.Close()
		}
	}
	for linha, qtde := range contagens {
		if qtde > 1 {
			fmt.Printf("%d\t%s\n", qtde, linha)
		}
	}
}

func contarLinhas(arquivo *os.File, contagens map[string]int) {
	entrada := bufio.NewScanner(arquivo)
	for entrada.Scan() {
		contagens[entrada.Text()]++
	}
	// NOTA: ignorar eventuais erros de from entrada.Err()
}

//!-
@claudioandre-br
Copy link
Author

A propósito, proponho que usemos CI no repositório da tradução. Desta forma, o compilador checará se o código fonte traduzido (ainda) está Ok.

Veja um exemplo aqui:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant