通過我的前端 (React) 上傳檔案時,Go 正在讀取 csv 檔案,然后在 JSON 回應中以結構化格式發送資料。
JSON 回應:
{
"Success": true,
"labels": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"data": [
{
"label": "",
"data": null,
"borderColor": "",
"backgroundColor": ""
},
{
"label": "Kyle",
"data": [
"1517",
"1689",
"1719",
"1591",
"1490",
"1310",
"1533",
"1500",
"1400",
"1300",
"1600",
"1800"
],
"borderColor": "#f6ff00",
"backgroundColor": "#f6ff00"
},
{
"label": "Mickey",
"data": [
"3000",
"3100",
"3200",
"3000",
"3500",
"4000",
"3700",
"3500",
"3000",
"4000",
"5000",
"4000"
],
"borderColor": "#ff0000",
"backgroundColor": "#ff0000"
},
{
"label": "Donald",
"data": [
"2500",
"2000",
"2800",
"3000",
"3100",
"2900",
"2800",
"3000",
"3200",
"3400",
"3500",
"3800"
],
"borderColor": "#001eff",
"backgroundColor": "#001eff"
}
]
}
您會看到問題是資料下的這一行:
{"label":"","data":null,"borderColor":"","backgroundColor":""}
我不知道為什么 Go 會添加這個
CSV 檔案:
,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Kyle,1517,1689,1719,1591,1490,1310,1533,1500,1400,1300,1600,1800
Mickey,3000,3100,3200,3000,3500,4000,3700,3500,3000,4000,5000,4000
Donald,2500,2000,2800,3000,3100,2900,2800,3000,3200,3400,3500,3800
func RouteUpload(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.ParseMultipartForm(16 * 1024 * 1024) // 16MB
file, handler, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
defer file.Close()
// Create an empty file on filesystem
f, err := os.OpenFile(filepath.Join("uploads", handler.Filename), os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
// Copy the file to the images directory
io.Copy(f, file)
w.Header().Set("Content-Type", "application/json")
type MyData struct {
Label string `json:"label"`
Data []string `json:"data"`
BorderColor string `json:"borderColor"`
BackgroundColor string `json:"backgroundColor"`
}
type MyFile struct {
Success bool `json:"Success"`
Labels []string `json:"labels"`
Data []MyData `json:"data"`
}
var myData MyData
var myFile MyFile
path := "uploads/" handler.Filename
// open file
fi, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
// remember to close the file at the end of the program
defer f.Close()
// read csv values using csv.Reader
csvReader := csv.NewReader(fi)
data, err := csvReader.ReadAll()
if err != nil {
fmt.Println(err)
}
colors := []string{"#00ff12", "#f6ff00", "#ff0000", "#001eff", "#ea00ff", "#ff9600"}
for i, line := range data {
if i == 0 {
for j, field := range line {
if j == 0 {
} else {
myFile.Labels = append(myFile.Labels, field)
}
}
} else {
fmt.Println(line)
for j, field := range line {
if j == 0 {
myData.Label = field
} else {
myData.Data = append(myData.Data, field)
}
}
myData.BorderColor = colors[i]
myData.BackgroundColor = colors[i]
}
myFile.Data = append(myFile.Data, myData)
myData.Data = []string{}
}
myFile.Success = true
// Marshal into JSON
responseJson, err := json.Marshal(myFile)
if err != nil {
fmt.Println(err)
}
// Send success response to user in JSON
fmt.Fprintf(w, "%s\n", responseJson)
}
uj5u.com熱心網友回復:
由于 ,您對 CSV 檔案的第一行的處理方式與其他行不同if i==0 {...} else {...},但之后您總是追加到- 所以對于 i==0,將追加myFile.Data空行。myData嘗試將此追加移動到else塊中。
其他一些建議:if ... else在 Go 中實際上是不鼓勵的(參見https://medium.com/@matryer/line-of-sight-in-code-186dd7cdea88)。所以對于,你可以通過在回圈之后使用if i==0來避免- 然后你可以將代碼移出塊。此外,當然應該是。elsecontinueforelseif j==0 {/*do nothing*/} else {/*do something*/}if j!=0 {/*do something*/}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430393.html
標籤:走
下一篇:goroutine如何設計場景
