I'd like to create a function which returns an integer value if a condition passes inside the function and a floating point number otherwise. How can I handle this with Go? Thank you!
3条回答 默认 最新
- dqj5046 2019-05-02 20:50关注
Yes, it is possible. You just have to return it as a float:
func doStuff(flag bool) float32 { if flag { return 1.1 } return 1 } func main() { num1 := doStuff(true) num2 := doStuff(false) fmt.Println("Num1: ", num1) fmt.Println("Num2: ", num2) }
The only thing that matters is how you format it for displaying.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报