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 }.

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

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况