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
83 changes: 83 additions & 0 deletions cur/gwenn/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down