Home > Backend Development > Golang > How to deal with the problem of too many return values ​​in golang

How to deal with the problem of too many return values ​​in golang

王林
Release: 2019-12-24 15:26:05
Original
2807 people have browsed it

How to deal with the problem of too many return values ​​in golang

Multiple value returns

A very special feature of Go (for a compiled language) is that functions and methods can return multiple values ​​( Python and Perl also work).

This can be used to improve a lot of bad conventions in C programs: modifying parameters and returning an error (for example, returning -1 if EOF is encountered). In Go, Write returns a count value and an error: "Yes, you wrote some bytes, but not all were written due to a device exception".

*File.Write in the os package is declared like this:

func (file *File) Write(b []byte) (n int, err error)
Copy after login

As the documentation states, it returns the number of bytes written, and when n != len(b) , returns a non-nil error. This is a common approach in Go. Tuples don't appear as native types, so multiple return values ​​may be the best choice. You can return exactly the value you want without having to overload the field space to a specific error signal.

Named return values

The return value or result parameter of a Go function can be assigned a name and used like an original variable, just like an input parameter. If they are named, they are initialized with the zero value of their type at the beginning of the function. If the function executes the return statement without adding parameters, the result parameters will be returned. Using this feature allows (again) to do more with less code.

Names are not mandatory, but they can make the code more robust and clear: here is the documentation. For example, naming the nextPos return value of type int can indicate which one represents which

func nextInt(b []byte, pos int) (value, nextPos int) { /* ... */ }
Copy after login

Since the named results will be initialized and associated with the unmodified return, they can be very simple and clear. Here is a piece of io.ReadFull code, which is used very well:

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:len(buf)]
    }
    return
}
Copy after login

Recommended related articles and tutorials: golang tutorial

The above is the detailed content of How to deal with the problem of too many return values ​​in golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template