dssjxvbv918586 2019-05-02 19:54
浏览 163
已采纳

是否有可能在Golang中返回Integer或Float的函数?

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.

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?