這是我的代碼片段,我不知道如何驗證 number1 和 number2 變數中的輸入資料型別。我需要它們是 float64 數字,但不是字串或其他型別。我讀過關于 try catch 的文章,但我不知道如何在這里使用它們。還是有更簡單的驗證方式?
package main
import (
"fmt"
"math"
)
func main() {
var number1, number2 float64
var operator string
fmt.Print("Enter the first number: ")
fmt.Scanln(&number1)
fmt.Print("Enter the second number: ")
fmt.Scanln(&number2)
fmt.Print("Enter the operator , -, *, /, **: ")
fmt.Scanln(&operator)
uj5u.com熱心網友回復:
float64您可以使用該strconf.ParseFloat函式檢查字串值是否正確
f, err := strconv.ParseFloat("4.56", 64)
if err != nil {
fmt.Println("The value is not a valid float!")
return
}
fmt.Print(f)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521187.html
標籤:验证去错误处理
