diff --git a/cur/ayag/go.mod b/cur/ayag/go.mod new file mode 100644 index 0000000..9cabb52 --- /dev/null +++ b/cur/ayag/go.mod @@ -0,0 +1,3 @@ +module cur + +go 1.25.6 diff --git a/cur/ayag/main.go b/cur/ayag/main.go new file mode 100644 index 0000000..e51bc22 --- /dev/null +++ b/cur/ayag/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "encoding/csv" + "fmt" + "os" + "strconv" + "sync" + "time" +) + +func processLine(line []string, result chan<- float64) { + time.Sleep(500 * time.Millisecond) + + price := line[2] + priceFloat, err := strconv.ParseFloat(price, 64) + if err != nil { + fmt.Println(err) + result <- 0.0 + return + } + result <- priceFloat +} + +func main() { + startTime := time.Now() + fileCSV, err := os.Open("../testcur.csv") + if err != nil { + fmt.Println(err) + return + } + + read := csv.NewReader(fileCSV) + lines, err := read.ReadAll() + if err != nil { + fmt.Println(err) + return + } + + total := make(chan float64) + var wg sync.WaitGroup + + rows := lines[1:] + + for _, row := range rows { + wg.Add(1) + + go func(r []string) { + defer wg.Done() + processLine(r, total) + }(row) + } + + go func() { + wg.Wait() + close(total) + }() + + var totals float64 + for price := range total { + totals += price + } + + fmt.Printf("Total: %f\n", totals) + fmt.Printf("Rows processed: %d\n", len(rows)) + fmt.Printf("Duration: %s\n", time.Since(startTime)) +}