I have written a script in Golang to sort user stories stored in a csv and create sprints with a fixed team velocity. The following is the part related to reading the csv file.
// userstory.go
f, err := os.Open("userstories.csv")
if err != nil {
log.Fatal(err)
} the file at the end of the program
defer f.Close()
csvReader := csv.NewReader(f)
data, err := csvReader.ReadAll()
Now, since I aim to make it work in the browser (with no backend) and the golang code is compiled to wasm, I can't use os files anymore.
So I'm trying to get the ArrayBuffer from the File and pass it as an Uint8Array argument (from JavaScript) to the function GetSprints:
// js code
const runScript = async () => {
let files = data.fileList;
files[0].arrayBuffer().then((x) => {
var b = new Uint8Array(x);
console.log(b)
console.log(GetSprints(b));
})
}
GetSprints has been exposed to JavaScript with the following js.Global().Set:
// userstory.go
func main() {
fmt.Println("Go WebAssembly: loaded UserStory.go")
js.Global().Set("GetSprints", getSprintsWrapper())
<-make(chan bool)
}
and the Uint8Array passed as first argument, is copied to a []byte buffer:
// userstory.go
func getSprintsWrapper() js.Func {
getSprintsFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
var dst []byte
js.CopyBytesToGo(dst, args[0])
return GetSprints(dst)
})
return getSprintsFunc
}
Given that csv.NewReader accepts a io.Reader as argument, i tried to replace the file descriptor with bytes.Reader
// userstory.go
func GetSprints(inputFile []byte) []Sprint {
f := bytes.NewReader(inputFile)
csvReader := csv.NewReader(f)
data, err := csvReader.ReadAll()
if err != nil {
log.Fatal(err)
}
fmt.Println("No errors reading csv. Data: ", data)
// ...
}
but it results in an empty array.

Someone else interested in parsing files with golang code compiled to wasm?
Thanks!

