dongtang6775 2013-03-25 20:44
浏览 20
已采纳

将字符串类型转换为int

Ok, so I've been having difficulties with the type conversion of a string to byte write. This is the compiler error:

cannot use row[5] (type uint8) as type string in function argument

cannot use &v (type *Field) as type int in function argument

This is an example of row[5]: $15,000.00

Ive declared a struct:

type Field struct {
Eia uint8
}

here is the main implementation:

for {
    record, err := reader.Read()
    if err == io.EOF {
        break
    } else if err != nil {
        panic(err)
    }

    var v Field
    for _, row := range record {
        eia, err := strconv.ParseInt(row[5], 8, &v) // Estimated Incoming Amount
        if err == nil {
            fmt.Printf("%+v
", v)
        } else {
            fmt.Println(err)
            fmt.Printf("%+v
", v)
        }

Can anyone please explain to me how strconv can convert the row to a integer?

  • 写回答

1条回答 默认 最新

  • duanlie7447 2013-03-25 21:18
    关注

    If you made a complete example on http://play.golang.org/ it'd be easier to give you a complete solution.

    ParseInt() takes the string (you might have to do string(row[5])), the base (you probably meant 10) and the bitsize (that's where you should put 8).

    It retuns an int (eia), it doesn't put it into the struct as it looks like you are trying.

    Instead do if err == nil { v.Eia = eia }.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?