Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cur/ayag/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cur

go 1.25.6
67 changes: 67 additions & 0 deletions cur/ayag/main.go
Original file line number Diff line number Diff line change
@@ -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))
}