# 提取方法

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/go/llms.txt).

> **TL;DR**
> `重构 | 提取 | 提取方法`
>
>
>
> `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+Alt+M` (VSCode), `⌘ ⌥ M` (VSCode (macOS))

通过   提取方法   重构，您可以选取一段代码，将其移动到单独的方法中，并用对该方法的调用替换旧代码。 如果代码始终以 return 语句退出（所有执行路径都可能通向隐式的 return 语句），则按原样提取。 但是，如果它包含没有 return 的执行路径，提取的方法将具有一个额外的  `bool`  标志作为  `return`  值。 该标志用于在调用端执行提前返回。

当您使用   提取方法   重构提取函数时，GoLand 会保留父函数参数的原始顺序。

Procedure: 提取函数和方法

1. 在编辑器中，选择您要提取的表达式或其部分。 您还可以将文本光标放在表达式内，在这种情况下，GoLand 会为您提供一个潜在代码选择列表。

2. 按 `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+Alt+M` (VSCode), `⌘ ⌥ M` (VSCode (macOS)) 或进入主菜单中的  `重构 | 提取 | 提取方法` 。

3. 输入方法名称并按 `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (VSCode), `⏎` (VSCode (macOS))。

[Video](https://resources.jetbrains.com/help/img/idea/2026.3/go_extract_method.mp4)

[GitHub 上的代码片段](https://github.com/apronichev/documentation-code-examples/blob/master/extractMethodWithReturn/main.go)

