func Delete_exam(UserId int,ExamId string){ //я передаю ExamId строкой и хочу перевести в int log.Println(ExamId ) //отображается число в кавычках , т.е. string тип var Exam_id_int int if Exam_id, err := strconv.Atoi(string(ExamId)); err == nil { fmt.Printf("Exam_id=%d, type: %T\n", Exam_id_int , Exam_id_int ) } //....... } 

I am 100% sure that there is a number inside the text, but it falls into the exception, i.e. can not convert. At the same time, if I forcibly set ExamId = "777", here the number is recognized and everything will be ok.
Maybe in ExamId there are some other characters, because of what he does not want to be transferred to an int? I hit my head against the wall, I can't understand

  • one
    Printed out what could not be converted tried? - Vladimir Martyanov
  • Thank you, it turned out - it turned out ExamId = "\" 1 \ "". I wrote it down, and then found out what was the matter .... - Rakzin Roman
  • The debugger is a very useful thing! - Vladimir Martyanov
  • one
    If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

1 answer 1

 func Delete_exam(UserId int,ExamId string){ log.Println(ExamId ) var Exam_id_int int if Exam_id, err := strconv.Atoi(string(ExamId)); err == nil { fmt.Printf("Exam_id=%d, type: %T\n", Exam_id_int , Exam_id_int ) }else{log.Println("Error-",err.Error())} //Показало ошибку } 

Thanks to Vladimir Martyanov. Brought just a mistake and looked at what was happening. It turned out that my ExamId was "\" 1 \ "", so the console was displayed and normal, but not converted. In general, I found a place where ExamId was saved, set without \ "and it all worked