Skip to content

Instantly share code, notes, and snippets.

@resilva87
Created May 27, 2021 23:36
Show Gist options
  • Save resilva87/fef1b4075784bc9ac3a2cdec388846be to your computer and use it in GitHub Desktop.
Save resilva87/fef1b4075784bc9ac3a2cdec388846be to your computer and use it in GitHub Desktop.
Template processar arquivo em Go
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
func fazOQueCeQuicer(){
}
func process(inputs []string, concurrencyLimit int) []bool {
semaphoreChan := make(chan struct{}, concurrencyLimit)
resultsChan := make(chan bool)
defer func() {
close(semaphoreChan)
close(resultsChan)
}()
for i, line := range inputs {
go func(i int, line string) {
semaphoreChan <- struct{}{}
fazOQueCeQuicer()
resultsChan <- true
<-semaphoreChan
}(i, line)
}
var results []bool
for {
result := <-resultsChan
results = append(results, result)
if len(results) == len(inputs) {
break
}
}
return results
}
func main() {
filename := os.Args[1]
file, err := os.Open(filename)
if err != nil {
fmt.Println(fmt.Errorf("%s\n", err))
os.Exit(-1)
}
defer file.Close()
var inputs []string
scanner := bufio.NewScanner(file)
// const maxCapacity = 4096 * 1024
// buf := make([]byte, maxCapacity)
// scanner.Buffer(buf, maxCapacity)
for scanner.Scan() {
line := scanner.Text()
inputs = append(inputs, line)
}
// if scanner.Err() != nil {
// fmt.Printf(" > Failed with error %v\n", scanner.Err())
// }
// fmt.Printf("%d\n", len(inputs))
concurrency := 100
process(inputs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment