diff --git a/cur/gwenn/main.go b/cur/gwenn/main.go new file mode 100644 index 0000000..6e89c3f --- /dev/null +++ b/cur/gwenn/main.go @@ -0,0 +1,83 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + "sync" + "time" +) + +// Result holds parsed cost info +type Result struct { + cost float64 +} + +// Worker function (Requirement #1) +func parseRow(row string, results chan<- Result, wg *sync.WaitGroup) { + defer wg.Done() + + // Simulate slow database + time.Sleep(100 * time.Millisecond) + + fields := strings.Split(row, ",") + if len(fields) < 3 { + return + } + + cost, err := strconv.ParseFloat(fields[2], 64) + if err != nil { + return + } + + results <- Result{cost: cost} +} + +func main() { + start := time.Now() + + file, err := os.Open("/home/gwenn/internship-samplecodes/cur/testcur.csv") + if err != nil { + panic(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + + // Skip header + scanner.Scan() + + results := make(chan Result) + var wg sync.WaitGroup + + rowsProcessed := 0 + totalCost := 0.0 + + // Collector goroutine + go func() { + for res := range results { + totalCost += res.cost + rowsProcessed++ + } + }() + + // Read file line by line + for scanner.Scan() { + line := scanner.Text() + + wg.Add(1) + go parseRow(line, results, &wg) + } + + // Wait for workers + wg.Wait() + close(results) + + elapsed := time.Since(start) + + fmt.Println("Rows processed:", rowsProcessed) + fmt.Printf("Total cost: %.2f\n", totalCost) + fmt.Println("Time taken:", elapsed) +} diff --git a/go.mod b/go.mod index f47a071..78eeba7 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/alphauslabs/internship-samplecodes -go 1.25 +go 1.22 require ( cloud.google.com/go/spanner v1.87.0