Handling Values in Function Calls in Conditional Statements
When evaluating the values of functions in conditional statements, it's essential to pay attention to the proper syntax. In Go, functions used in expressions must return a value.
Consider the following example:
package main import "fmt" func main(){ if sumThis(1,2) > sumThis(3,4){ fmt.Println("test") } else { fmt.Println("derp") } } func sumThis(a, b int){ return a+b }
This code returns an error because the sumThis function is not declared to return a value. To resolve this, you should explicitly declare a return type for the function. Here's the corrected code:
func sumThis(a, b int) int { return a+b }
With the proper return type declared, the code will now compile and print the appropriate message based on the comparison of the function values.
The above is the detailed content of How to Correctly Handle Function Return Values in Go Conditional Statements?. For more information, please follow other related articles on the PHP Chinese website!