strconv

strconv.Atoi in Go (Basic calculator)

假装没事ソ 提交于 2021-02-08 11:26:29
问题 I'm trying to make a basic adding calculator in Go (complete noob here), but every time I'm getting an output of 0. This is the code: package main import ( "fmt" "strconv" //"flag" "bufio" "os" ) func main(){ reader := bufio.NewReader(os.Stdin) fmt.Print("What's the first number you want to add?: ") firstnumber, _ := reader.ReadString('\n') fmt.Print("What's the second number you want to add?: ") secondnumber, _ := reader.ReadString('\n') ifirstnumber, _ := strconv.Atoi(firstnumber)

strconv.Atoi in Go (Basic calculator)

醉酒当歌 提交于 2021-02-08 11:26:16
问题 I'm trying to make a basic adding calculator in Go (complete noob here), but every time I'm getting an output of 0. This is the code: package main import ( "fmt" "strconv" //"flag" "bufio" "os" ) func main(){ reader := bufio.NewReader(os.Stdin) fmt.Print("What's the first number you want to add?: ") firstnumber, _ := reader.ReadString('\n') fmt.Print("What's the second number you want to add?: ") secondnumber, _ := reader.ReadString('\n') ifirstnumber, _ := strconv.Atoi(firstnumber)

Detect value out of range errors using type assertion in Golang

核能气质少年 提交于 2019-12-08 07:16:21
问题 Given the following code: iv, err := strconv.ParseInt("18446744073709551448", 10, 64) fmt.Println(iv) fmt.Printf("%#v\n", err) fmt.Printf("%v\n", err) //Output: 9223372036854775807 &strconv.NumError{Func:"ParseInt", Num:"18446744073709551448", Err:(*errors.errorString)(0x1040a040)} strconv.ParseInt: parsing "18446744073709551448": value out of range How can I detect that the function failed due to being out of range of an int64? The strconv.ParseInt function returns an error type, but in this

String to Float64: multiple-value strconv.ParseFloat() in single-value context

纵饮孤独 提交于 2019-12-02 17:26:52
问题 I have an array of STRING slices like this: [[header1 header2 startdate enddate header3 header4] [item1 100 01/01/2017 02/01/2017 5343340.56343 3.77252223956] [item2 554 01/01/2017 02/01/2017 22139.461201388 17.232284405]] Keep in mind that the array keeps on increasing. I am just posting a sample array. Now I converted some of the float numbers to string so that I could append it to the string slices. However, I need to do some math with those numbers. I want to add the string number(5343340