Is unnamed arguments a thing in Go?

前端 未结 2 1790
小蘑菇
小蘑菇 2020-12-03 05:44

I am writing a parser in Go for Go, and to test it I downloaded a bunch of files from github projects.
In https://github.com/andlabs/ui I bumped into a file containing t

2条回答
  •  情深已故
    2020-12-03 06:00

    The purpose of unnamed function arguments is for arguments (which are local variables of the function) which are not referred to in the function's code, and therefore do not need a name. An interesting note about anonymous variables is that they are actually used more commonly than you may think. In Go, a function's return value(s) are usually listed as types, but actually they are also local variables of the function which can be named and manipulated.

    See this example in the "Effective Go" page on golang.org https://golang.org/doc/effective_go.html#named-results

    func ReadFull(r Reader, buf []byte) (n int, err error) {
        for len(buf) > 0 && err == nil {
            var nr int
            nr, err = r.Read(buf)
            n += nr
            buf = buf[nr:]
        }
        return
    }
    

提交回复
热议问题