dousi8237 2019-06-23 23:11
浏览 86
已采纳

无法将字符串解析到数据存储区

With reference to this question, I am trying to convert a datastore key from the Form to int64 and with the help of that key, trying to update a record in the datastore. I tried exactly the same code as in the answer selected in the reference link, but I get an error which says, Unable to parse key. strconv.ParseInt: parsing "/Users,6248007768440574": invalid syntax

The value I'm passing from the form is a datastore key of the format /Users,6248007768440574. I realize that the syntax error is because of this. Could anyone please help me in how to go about with this issue? Or, is there a method to convert a string to *datastore.Key? Any help will be appreciated

  • 写回答

1条回答 默认 最新

  • dongleman4760 2019-06-24 00:55
    关注

    ParseInt returns an error because the string "/Users,6248007768440574" is not an integer. A substring is an integer, but not the entire string.

    The string in the question was created by calling Key.String. The datastore package does not provide a way to parse the output from this method. Here's a simple parser for the case where there are no ancestor keys:

    var keyPat = regexp.MustCompile(`^/([^\.]*),(\d+)$`)
    
    func parseKey(s string) (*datastore.Key, error) {
        m := keyPat.FindStringSubmatch(s)
        i := strings.Index(s, ",")
        if i < 0 {
            return nil, errors.New("bad format")
        }
        n, err := strconv.ParseInt(m[2], 10, 64)
        if err != nil {
            return nil, err
        }
        return datastore.IDKey(m[1], n, nil), nil
    }
    

    The the Encode method and Decode function are the preferred way to convert a key to and from a machine readable string.

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

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了