Does the Go language have function/method overloading?

后端 未结 3 1714
礼貌的吻别
礼貌的吻别 2020-12-01 11:51

I\'m porting a C library to Go. A C function (with varargs) is defined like this:

curl_easy_setopt(CURL *curl, CURLoption option, ...); 

So

相关标签:
3条回答
  • 2020-12-01 12:03

    func (e *Easy)SetOption(any []interface{})

    The process converts the parameters to this- empty interface{} .

    The first type of conversion, and then the internal logic processes.

    http://zerousm99.blogspot.kr/2015/01/golang-overload.html

    0 讨论(0)
  • 2020-12-01 12:15

    No it does not.

    See the Go Language FAQ, and specifically the section on overloading.

    Method dispatch is simplified if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system.

    Update: 2016-04-07

    While Go still does not have overloaded functions (and probably never will), the most useful feature of overloading, that of calling a function with optional arguments and inferring defaults for those omitted can be simulated using a variadic function, which has since been added. But this comes at the loss of type checking.

    For example: http://changelog.ca/log/2015/01/30/golang

    0 讨论(0)
  • 2020-12-01 12:18

    According to this, it doesn't: http://golang.org/doc/go_for_cpp_programmers.html

    In the Conceptual Differences section, it says:

    "Go does not support function overloading and does not support user defined operators."

    0 讨论(0)
提交回复
热议问题