GOCSV映射到带日期/时间的结构:字段为空时发出

GOCSV映射到带日期/时间的结构:字段为空时发出,csv,datetime,go,Csv,Datetime,Go,编辑:我犯了一个错误并更正了它 我正在尝试将CSV文件映射到定义time.Datetime字段的结构(稍后我想将Datetime格式更改为另一种格式) 这是我程序的输出 ❯ go run issue.go ✗ m

编辑:我犯了一个错误并更正了它

我正在尝试将
CSV
文件映射到定义
time.Datetime
字段的结构(稍后我想将Datetime格式更改为另一种格式)

这是我程序的输出

❯ go run issue.go                                                                                                                                                 ✗ master
record:  05/02/2019 15:02:31
record:  05/02/2019 16:02:40
record:
record on line 0; parse error on line 4, column 2: parsing time "" as "02/01/2006 15:04:05": cannot parse "" as "02"
1936  -  2019-02-05 15:02:31 +0000 UTC
1937  -  2019-02-05 16:02:40 +0000 UTC
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x10e58cc]

goroutine 1 [running]:
main.main()
        /Users/gtheys/Code/go/src/github.axa.com/KTAXA/claims-headers-poc/issue.go:49 +0x1bc
exit status 2
我试图执行
时间。使用空字符串解析
,但无效。所以问题应该在这里。但我不知道如何在以下代码中处理它:

package main

import (
    "encoding/csv"
    "fmt"
    "io"
    "os"
    "time"

    "github.com/gocarina/gocsv"
)

type DateTime struct {
    time.Time
}

type Issue struct {
    NotifyNo   string
    NotifyDate DateTime
}

// Convert the CSV string as internal date
func (date *DateTime) UnmarshalCSV(csv string) (err error) {
    fmt.Println("record: ", csv)
    date.Time, err = time.Parse("02/01/2006 15:04:05", csv)
    return err
}

func main() {
    //const layout = "02/05/2006 15:04:00"
    issuesFile, err := os.OpenFile("ISSUES.txt", os.O_RDWR|os.O_CREATE, os.ModePerm)
    if err != nil {
        panic(err)
    }
    defer issuesFile.Close()

    issues := []*Issue{}

    gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {
        r := csv.NewReader(in)
        r.Comma = '|'
        return r // Allows use pipe as delimiter
    })

    if err := gocsv.UnmarshalFile(issuesFile, &issues); err != nil {
        fmt.Println(err)
    }
    for _, issue := range issues {
        fmt.Println(issue.NotifyNo, " - ", issue.NotifyDate)
    }

    if _, err := issuesFile.Seek(0, 0); err != nil { // Go to the start of the file
        panic(err)
    }
}
csv输入文件:

NotifyNo|NotifyDate|
1936|05/02/2019 15:02:31|
1937|05/02/2019 16:02:40|
1938||
1951|05/02/2019 17:02:19|
“我试图执行
时间。使用空字符串解析
,它成功了。”与我看到的不一致,你确定它对你有效吗?无论如何,要解决这个问题,只需检查
csv
字符串是否为空,如果为空,则跳过
时间。解析
步骤并返回
nil
。好的,我在时间之前做了一个So。解析我必须检查类型并将其转换为字符串才能工作?我至少会试试看