முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 3 · பாடம் 2 இன் 5பாடத்திட்டத்தில் 12/50~12 min
தொகுதி பாடங்கள் (2/5)

பல வருமானங்கள் மற்றும் பெயரிடப்பட்ட வருமானங்கள்

In Go, a function can return multiple values. It's the mechanism that enables the (value, error) pattern you've already seen and that pervades the entire standard library.

Multiple returns

The return types go between parentheses, comma-separated:

Go
func divmod(a, b int) (int, int) {
    return a / b, a % b
}

q, r := divmod(17, 5) // q=3, r=2

They are destructured with multiple assignment. To discard one of the values use _:

Go
_, r := divmod(17, 5) // solo il resto

The canonical (T, error) pattern

Almost all of the stdlib follows it. error is ALWAYS last:

Go
func read(path string) ([]byte, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, fmt.Errorf("read %q: %w", path, err)
    }
    return data, nil
}

Named returns

You can name the return values: they become pre-declared variables initialized to zero, and return with no arguments returns them ("naked return"):

Go
func divmod(a, b int) (q, r int) {
    q = a / b
    r = a % b
    return       // naked: ritorna q, r
}

Named returns also appear in documentation tooltips, so they're useful as a form of inline documentation for complex functions — not just for the naked return.

More than two returns?

Nothing stops you from (int, int, int, error), but if you feel the need it's almost always a sign that you should return a struct:

Go
type Result struct {
    Sum  int
    Avg  float64
    Max  int
}

func analyze(nums []int) (Result, error) { ... }

Try it

உடற்பயிற்சி#go.m3.l2.e1
முயற்சிகள்: 0ஏற்றுகிறது…

Define divmod(a, b int) (int, int) that returns quotient and remainder.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Return types between parentheses: `(int, int)`.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

உடற்பயிற்சி#go.m3.l2.e2
முயற்சிகள்: 0ஏற்றுகிறது…

Implement safeDiv(a, b int) (int, error): if b == 0 return (0, errors.New('divisione per zero')), otherwise (a/b, nil).

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Error ALWAYS as the last value. On error: zero value + error.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Quiz#go.m3.l2.e3
தயார்

What does this program print, using named returns?

Go
func f() (a, b int) {
    a = 1
    b = 2
    return
}
x, y := f()
fmt.Println(x, y)
பதில் விருப்பங்கள்

Recap

  • Multiple values between parentheses: func f() (T1, T2, error).
  • Convention: error ALWAYS as the last value; on error, zero value + error.
  • Named returns pre-declare the variables and enable naked returns.
  • Naked returns only in very short functions; beyond that, readability suffers.
  • More than 2-3 returns? You probably want a struct.