Alternative for function overloading in Go?

后端 未结 4 1504
不知归路
不知归路 2021-02-19 16:14

Is it possible to work similar way like the function overloading or optional parameter in C# using Golang? Or maybe an alternative way?

4条回答
  •  有刺的猬
    2021-02-19 17:05

    The idiomatic answer to optional parameters in Go is wrapper functions:

    func do(a, b, c int) {
        // ...
    }
    
    func doSimply(a, b) {
        do(a, b, 42)
    }
    

    Function overloading was intentionally left out, because it makes code hard(er) to read.

提交回复
热议问题