Wednesday, 15 April 2015

go - How to check if Scanln throws an error in Golang -


I am new to go. I was searching for answers and I know that there is really one that I have have not received.

To better explain my question, this is my code:

  func main () {... inputs: = new (inputs) fmt.println ("Input two numbers:") fmt.Scanln (& inputs.A) fmt.Scanln (and inputs.B) fmt.Println ("sum:", inputs.A + inputs.B)}  < / Pre> 

And here is my straight:

  Type input structure {A, B int}  

If I input to input A ' 123 'and the second' 123 'for input B, I have an output of " is sum: 246 " But if I accidentally input '123j' , then it will not work anymore because A and B only accept the int (s).

Now, how can terror hold with fmt.Scanln or any other way? thank you in advanced.

... do not ignore them .

You are ignoring two important return values. Scan and count of fault .. If there was one.

  n, err: = fmt.Scanln (and inputs.A)  

... will tell you what to check. mistake will tell you that a new line was expected and it was not found because you tried to store the value in int .. and it


No comments:

Post a Comment