I have a simple question - can I use single value assignment while method returns 2 values (val and error)?
resp := http.Get("http://www.google.com/")
I have a simple question - can I use single value assignment while method returns 2 values (val and error)?
resp := http.Get("http://www.google.com/")
The number of operands on the left side of the assignment must match the number of values returned by the function.
You can use the blank identifier to ignore a return value:
resp, _ := http.Get("http://www.google.com/")
It's bad practice to ignore errors like this.
From Go language specification :
A tuple assignment assigns the individual elements of a multi-valued operation to a list of variables. There are two forms. In the first, the right hand operand is a single multi-valued expression such as a function call, a channel or map operation, or a type assertion. The number of operands on the left hand side must match the number of values.
For instance, if f is a function returning two values,
x, y = f() assigns the first value to x and the second to y.
The blank identifier provides a way to ignore right-hand side values in an assignment:
_ = x // evaluate x but ignore it
x, _ = f() // evaluate f() but ignore second result value