Skip to content

Instantly share code, notes, and snippets.

@marclave
Created September 28, 2016 19:16
Show Gist options
  • Save marclave/28446dfd937916619ba65a51cbaff583 to your computer and use it in GitHub Desktop.
Save marclave/28446dfd937916619ba65a51cbaff583 to your computer and use it in GitHub Desktop.
GO Channel example with parsing a file
// This function is to read the file line and parse the desired info
func sanitizeString(line string, zone string) string {
return domain
}
func parseLine(id int, messages <-chan string, wg *sync.WaitGroup, results chan<- int) {
defer wg.Done()
for line := range messages {
// Handle the message, update the DB if desired
}
}
func main() {
path := flag.String("p", ".domains", "Path to Zone file")
workers := flag.Int("w", 2000, "Number of workers to work")
zone := flag.String("z", ".nil", "Zone specified")
flag.Parse()
fmt.Println("STARTING")
inFile, _ := os.Open(*path)
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
// Make channels for concurrency
messages := make(chan string)
results := make(chan int, 100)
wg := new(sync.WaitGroup)
// Adding the function for the workers to use
for w := 1; w <= *workers; w++ {
wg.Add(1)
go parseLine(w, messages, wg, results)
}
go func() {
for scanner.Scan() {
//messages <- scanner.Text()
domain := sanitizeString(scanner.Text(), *zone)
if domain == "" {
continue
}
// PASS LINE INTO CHANNEL FOR WORKERS TO USE
messages <- domain
}
close(messages)
}()
// Wait until workers are done
go func() {
wg.Wait()
close(results)
}()
// Counts for dummy results
counts := 0
for v := range results {
counts += v
}
fmt.Println("DONE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment