drzyeetvt41077335 2016-04-29 00:12
浏览 1225
已采纳

给定字符串时,strconv.Atoi()抛出错误

When trying to use strconv on a variable passed via URL(GET variable named times), GoLang fails on compilation stating the following:

multiple-value strconv.Atoi() in a single-value context

However, when I do reflect.TypeOf I get string as the type, which to my understanding is the correct type of argument.

I have been trying to fix this issue for several hours. I'm new to go and have become pretty frustrated with this problem. I finally decided to ask for help. Any feedback would be appreciated.

func numbers(w http.ResponseWriter, req *http.Request) {
  fmt.Println("GET params were:", req.URL.Query()); 
  times := req.URL.Query()["times"][0]
  time := strconv.Atoi(times)

  reflect.TypeOf(req.URL.Query()["times"][0]) // returns string
}
  • 写回答

2条回答 默认 最新

  • doushi9474 2016-04-29 00:22
    关注

    The error is telling you that the two return values from strconv.Atoi (int and error) are used in a single value context (the assignment to time). Change the code to:

       time, err := strconv.Atoi(times)
       if err != nil {
          // handle error
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改