# 命名返回值

URL: https://caijiao.org/go/01-go-basic/14-named-return
Source: docs/go/01-go-basic/14-named-return.md
Description: 本节介绍 Go 语言中的命名返回值，用于简化函数返回，提升代码可读性和结构清晰度。

命名返回值是在函数声明时为返回值指定名字，函数体内可以直接赋值并用裸 return 返回。

## 1. 命名返回值定义

```go
func calculate(a, b int) (sum int, diff int) {
    sum = a + b
    diff = a - b
    return
}
````

## 2. 使用示例

调用：

```go
s, d := calculate(10, 5)
fmt.Println("和:", s)    // 输出：和: 15
fmt.Println("差:", d)    // 输出：差: 5
```

## 3. 裸 return

函数体内没有显式返回值，直接使用 `return` 返回命名结果：

```go
func split(str string) (first, second string) {
    parts := strings.SplitN(str, " ", 2)
    first = parts[0]
    if len(parts) > 1 {
        second = parts[1]
    }
    return
}
```

---

命名返回值让函数返回更直观，适合返回多个值的场景。
