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.

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

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害