<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Posts on 我的博客</title><link>https://d25077da.cuiyoupeng.pages.dev/posts/</link><description>Recent content in Posts on 我的博客</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Mon, 11 Aug 2025 17:22:26 +0800</lastBuildDate><atom:link href="https://d25077da.cuiyoupeng.pages.dev/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>AI对话-gitignore用法</title><link>https://d25077da.cuiyoupeng.pages.dev/posts/other/ai%E5%AF%B9%E8%AF%9D-gitignore%E7%94%A8%E6%B3%95/</link><pubDate>Mon, 11 Aug 2025 17:22:26 +0800</pubDate><guid>https://d25077da.cuiyoupeng.pages.dev/posts/other/ai%E5%AF%B9%E8%AF%9D-gitignore%E7%94%A8%E6%B3%95/</guid><description>&lt;h3 id="git-忽略-confconfini-未生效问题排查与修复指引">Git 忽略 &lt;code>conf/conf.ini&lt;/code> 未生效问题排查与修复指引&lt;/h3>
&lt;h4 id="背景症状">背景/症状&lt;/h4>
&lt;ul>
&lt;li>你在 &lt;code>.gitignore&lt;/code> 中已添加了 &lt;code>conf/conf.ini&lt;/code> 的忽略规则，但 &lt;code>git status&lt;/code> 仍显示该文件有变更。&lt;/li>
&lt;/ul>
&lt;h4 id="根因">根因&lt;/h4>
&lt;ul>
&lt;li>&lt;code>.gitignore&lt;/code> 只对“未被跟踪”的文件生效。&lt;code>conf/conf.ini&lt;/code> 之前已经被提交（被 Git 索引跟踪），因此忽略规则对它不起作用。&lt;/li>
&lt;/ul>
&lt;h4 id="快速诊断">快速诊断&lt;/h4>
&lt;ul>
&lt;li>查看 &lt;code>.gitignore&lt;/code> 中确有规则（多处重复无碍但冗余）&lt;/li>
&lt;li>确认文件已被跟踪：
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;p>git ls-files conf/conf.ini&lt;/p>
&lt;pre tabindex="0">&lt;code>若输出包含 `conf/conf.ini`，说明该文件已被跟踪。
- 当前状态：
```bash
git status --porcelain
&lt;/code>&lt;/pre>&lt;p>会看到该文件被标记为修改（如 &lt;code>M conf/conf.ini&lt;/code>）。&lt;/p>
&lt;h4 id="解决步骤">解决步骤&lt;/h4>
&lt;ol>
&lt;li>从 Git 索引中移除该文件，但保留本地文件：
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ol>
&lt;p>git rm &amp;ndash;cached conf/conf.ini&lt;/p>
&lt;pre tabindex="0">&lt;code>2. 之后 `.gitignore` 的忽略规则将开始生效。
#### 验证
- 再次查看状态：
```bash
git status
&lt;/code>&lt;/pre>&lt;p>会看到 &lt;code>deleted: conf/conf.ini&lt;/code> 作为待提交变更（这表示从版本库删除，但不会删除你本地工作区文件）。&lt;/p>
&lt;h4 id="提交变更可选但推荐">提交变更（可选，但推荐）&lt;/h4>
&lt;ul>
&lt;li>提交索引移除操作，让团队成员也不再跟踪该文件：
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;p>git commit -m &amp;ldquo;chore: stop tracking conf/conf.ini and respect .gitignore&amp;rdquo;&lt;/p></description></item><item><title>AI对话-边车模式和服务网格</title><link>https://d25077da.cuiyoupeng.pages.dev/posts/other/ai%E5%AF%B9%E8%AF%9D-%E8%BE%B9%E8%BD%A6%E6%A8%A1%E5%BC%8F%E5%92%8C%E6%9C%8D%E5%8A%A1%E7%BD%91%E6%A0%BC/</link><pubDate>Mon, 11 Aug 2025 16:41:26 +0800</pubDate><guid>https://d25077da.cuiyoupeng.pages.dev/posts/other/ai%E5%AF%B9%E8%AF%9D-%E8%BE%B9%E8%BD%A6%E6%A8%A1%E5%BC%8F%E5%92%8C%E6%9C%8D%E5%8A%A1%E7%BD%91%E6%A0%BC/</guid><description>&lt;h3 id="服务网格和边车模式什么意思">服务网格和边车模式什么意思？&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>边车模式（Sidecar Pattern）&lt;/p>
&lt;ul>
&lt;li>将日志、流量控制、服务注册、熔断限流等“控制类”能力，从业务进程中剥离到独立的边车进程/容器，与主服务同生命周期运行。&lt;/li>
&lt;li>好处：业务与控制解耦，主服务只写业务逻辑。&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>服务网格（Service Mesh）&lt;/p>
&lt;ul>
&lt;li>在大规模微服务中，为每个服务配一个边车代理，所有边车组成网状网络。&lt;/li>
&lt;li>数据平面：各个 sidecar 处理转发、限流、熔断、TLS、可观测性等。&lt;/li>
&lt;li>控制平面：集中下发策略与配置，统一路由、限流、流量治理等。&lt;/li>
&lt;li>对应用零侵入，开发只写业务。&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>一句话：边车模式是“给每个服务配助手”，服务网格是“让所有助手组成智能网络”。&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="在-go-后端服务里边车模式怎么和业务代码工作需要开放统一端口吗">在 Go 后端服务里，边车模式怎么和业务代码工作？需要开放统一端口吗？&lt;/h3>
&lt;ul>
&lt;li>典型方式：主服务监听本地端口（如 &lt;code>:8080&lt;/code>），sidecar 作为反向代理监听对外端口（如 &lt;code>:8081&lt;/code>），两者通过 &lt;code>localhost&lt;/code> 通信。&lt;/li>
&lt;li>Go 主服务仅需像平时一样启动 HTTP 服务，不需要关心认证、限流、日志等控制逻辑。&lt;/li>
&lt;/ul>
&lt;p>示例（主服务，仅处理业务）：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">package&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;log&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;net/http&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">mux&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">http&lt;/span>.&lt;span style="color:#a6e22e">NewServeMux&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">mux&lt;/span>.&lt;span style="color:#a6e22e">HandleFunc&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;/ping&amp;#34;&lt;/span>, &lt;span style="color:#66d9ef">func&lt;/span>(&lt;span style="color:#a6e22e">w&lt;/span> &lt;span style="color:#a6e22e">http&lt;/span>.&lt;span style="color:#a6e22e">ResponseWriter&lt;/span>, &lt;span style="color:#a6e22e">r&lt;/span> &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">http&lt;/span>.&lt;span style="color:#a6e22e">Request&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">w&lt;/span>.&lt;span style="color:#a6e22e">Write&lt;/span>([]byte(&lt;span style="color:#e6db74">&amp;#34;pong&amp;#34;&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">log&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;app listening on :8080&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">http&lt;/span>.&lt;span style="color:#a6e22e">ListenAndServe&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;:8080&amp;#34;&lt;/span>, &lt;span style="color:#a6e22e">mux&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>示例（sidecar，反向代理到主服务）：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">package&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;net/http&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;net/http/httputil&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;net/url&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">target&lt;/span>, &lt;span style="color:#a6e22e">_&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">url&lt;/span>.&lt;span style="color:#a6e22e">Parse&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;http://127.0.0.1:8080&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">proxy&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">httputil&lt;/span>.&lt;span style="color:#a6e22e">NewSingleHostReverseProxy&lt;/span>(&lt;span style="color:#a6e22e">target&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">http&lt;/span>.&lt;span style="color:#a6e22e">ListenAndServe&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;:8081&amp;#34;&lt;/span>, &lt;span style="color:#a6e22e">proxy&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>在 Kubernetes 中，二者位于同一 Pod，共享网络命名空间，sidecar 直接转发到 &lt;code>127.0.0.1:8080&lt;/code>。&lt;/li>
&lt;li>使用服务网格（如 Istio）时，Envoy 会自动作为 sidecar 注入并接管流量，Go 服务只需监听本地端口。&lt;/li>
&lt;/ul>
&lt;h3 id="8080-是跟-sidecar-约定的吗如果没有约定端口sidecar-怎么跟-go-服务通信">8080 是跟 sidecar 约定的吗？如果没有约定端口，sidecar 怎么跟 Go 服务通信？&lt;/h3>
&lt;p>不是“天然约定”，而是部署时显式声明。常见方式：&lt;/p></description></item><item><title>Go singleflight 源码分析</title><link>https://d25077da.cuiyoupeng.pages.dev/posts/go/ai%E5%AF%B9%E8%AF%9D-singleflight%E5%AE%9E%E7%8E%B0/</link><pubDate>Thu, 07 Aug 2025 17:40:00 +0800</pubDate><guid>https://d25077da.cuiyoupeng.pages.dev/posts/go/ai%E5%AF%B9%E8%AF%9D-singleflight%E5%AE%9E%E7%8E%B0/</guid><description>&lt;hr>
&lt;p>&lt;strong>User&lt;/strong>&lt;/p>
&lt;p>这是什么原因&lt;/p>
&lt;hr>
&lt;p>&lt;strong>Cursor&lt;/strong>&lt;/p>
&lt;p>我需要查看当前打开的文件内容以及相关的错误信息来帮助您诊断问题。让我先检查一下您当前正在查看的文件。&lt;/p>
&lt;p>我看到您的光标在第73行，这是一个panic错误处理的位置。让我检查一下是否有任何编译错误或运行时错误信息。&lt;/p>
&lt;p>让我检查一下项目的构建状态和可能的错误：&lt;/p>
&lt;hr>
&lt;p>&lt;strong>User&lt;/strong>&lt;/p>
&lt;p>翻一下&lt;/p>
&lt;hr>
&lt;p>&lt;strong>Cursor&lt;/strong>&lt;/p>
&lt;p>我看到您在查看 &lt;code>singleflight&lt;/code> 包的代码。让我帮您翻译一下您选中的这段注释：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Do executes and returns the results of the given function, making&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// sure that only one execution is in-flight for a given key at a&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// time. If a duplicate comes in, the duplicate caller waits for the&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// original to complete and receives the same results.&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// The return value shared indicates whether v was given to multiple callers.&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>中文翻译：&lt;/strong>&lt;/p></description></item><item><title>Go 内存分配、逃逸分析与优化机制详解</title><link>https://d25077da.cuiyoupeng.pages.dev/posts/go/go%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D%E9%80%83%E9%80%B8%E5%88%86%E6%9E%90%E4%B8%8E%E4%BC%98%E5%8C%96%E6%9C%BA%E5%88%B6%E8%AF%A6%E8%A7%A3/</link><pubDate>Sun, 27 Apr 2025 16:05:00 +0800</pubDate><guid>https://d25077da.cuiyoupeng.pages.dev/posts/go/go%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D%E9%80%83%E9%80%B8%E5%88%86%E6%9E%90%E4%B8%8E%E4%BC%98%E5%8C%96%E6%9C%BA%E5%88%B6%E8%AF%A6%E8%A7%A3/</guid><description>&lt;hr>
&lt;h2 id="1-interface-的类型断言与底层实现">1. interface{} 的类型断言与底层实现&lt;/h2>
&lt;h3 id="11-类型断言的语法和用途">1.1 类型断言的语法和用途&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>&lt;code>input.(type)&lt;/code> 只能用于 type switch 语句中，用于判断 interface{} 变量的实际类型，并做类型分支处理。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>例如：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">var&lt;/span> &lt;span style="color:#a6e22e">input&lt;/span> &lt;span style="color:#66d9ef">interface&lt;/span>{} = &lt;span style="color:#ae81ff">123&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">switch&lt;/span> &lt;span style="color:#a6e22e">v&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">input&lt;/span>.(&lt;span style="color:#66d9ef">type&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">case&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// v 是 int 类型&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">case&lt;/span> &lt;span style="color:#66d9ef">string&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// v 是 string 类型&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;h3 id="12-interface-的底层结构">1.2 interface{} 的底层结构&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>interface{} 在 Go 底层实现为一个结构体，包含两个指针：&lt;/p>
&lt;ul>
&lt;li>&lt;code>_type&lt;/code>：指向类型元数据（类型描述信息）&lt;/li>
&lt;li>&lt;code>data&lt;/code>：指向实际数据的指针&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">type&lt;/span> &lt;span style="color:#a6e22e">eface&lt;/span> &lt;span style="color:#66d9ef">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">_type&lt;/span> &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">_type&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">data&lt;/span> &lt;span style="color:#a6e22e">unsafe&lt;/span>.&lt;span style="color:#a6e22e">Pointer&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;h3 id="13-类型断言的底层实现">1.3 类型断言的底层实现&lt;/h3>
&lt;ul>
&lt;li>当你写 &lt;code>input.(type)&lt;/code> 或 &lt;code>input.(SomeType)&lt;/code> 时，Go 运行时会：
&lt;ol>
&lt;li>读取 interface{} 变量的 &lt;code>_type&lt;/code> 指针。&lt;/li>
&lt;li>将其与 case 分支中的类型元数据做比较。&lt;/li>
&lt;li>匹配则进入对应分支，并将 data 指针转换为目标类型。&lt;/li>
&lt;li>不匹配则继续下一个 case。&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>相关源码在 Go 的 runtime 包（如 &lt;code>iface.go&lt;/code>）中，类型断言的核心函数有 &lt;code>runtime.assertE2I&lt;/code>、&lt;code>runtime.assertE2T&lt;/code> 等。&lt;/li>
&lt;/ul>
&lt;h3 id="14-性能影响">1.4 性能影响&lt;/h3>
&lt;ul>
&lt;li>类型断言的性能损耗极小，通常只涉及指针比较和类型表查找。&lt;/li>
&lt;li>只有在极端高频调用下才可能成为瓶颈，绝大多数业务场景可以放心使用。&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="2-interface-数据的存储位置">2. interface{} 数据的存储位置&lt;/h2>
&lt;h3 id="21-interface-变量本身">2.1 interface{} 变量本身&lt;/h3>
&lt;ul>
&lt;li>interface{} 变量本身（即 eface 结构体）通常分配在栈上。&lt;/li>
&lt;/ul>
&lt;h3 id="22-interface-指向的数据">2.2 interface{} 指向的数据&lt;/h3>
&lt;ul>
&lt;li>interface{} 指向的数据，可能分配在栈上，也可能分配在堆上，&lt;strong>由逃逸分析决定&lt;/strong>。&lt;/li>
&lt;li>逃逸分析会根据变量的生命周期和使用方式，自动决定分配位置。&lt;/li>
&lt;/ul>
&lt;h3 id="23-影响逃逸的常见场景">2.3 影响逃逸的常见场景&lt;/h3>
&lt;ul>
&lt;li>如果 interface{} 指向的数据只在当前函数内使用，且没有被返回或跨 goroutine 传递，通常会分配在栈上。&lt;/li>
&lt;li>如果 interface{} 指向的数据被返回、传递到其他 goroutine、或生命周期不明确，则会分配到堆上。&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="3-逃逸分析与分配规则">3. 逃逸分析与分配规则&lt;/h2>
&lt;h3 id="31-逃逸分析的作用">3.1 逃逸分析的作用&lt;/h3>
&lt;ul>
&lt;li>Go 编译器通过逃逸分析（escape analysis）判断变量是否需要分配到堆上。&lt;/li>
&lt;li>逃逸分析的目标是尽量减少堆分配，提高程序性能。&lt;/li>
&lt;/ul>
&lt;h3 id="32-逃逸分析的常见规则">3.2 逃逸分析的常见规则&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>struct 返回值&lt;/strong>：大概率逃逸到堆上，尤其是大 struct 或返回指针时，但有优化空间（如返回值优化）。&lt;/li>
&lt;li>&lt;strong>普通类型返回值&lt;/strong>：如 int、float、bool 等，通常分配在栈上，除非返回的是指针。&lt;/li>
&lt;li>&lt;strong>interface{} 返回值&lt;/strong>：即使是普通类型，也可能逃逸到堆上，因为 interface{} 需要存储类型信息和数据指针，编译器有时会保守处理。&lt;/li>
&lt;/ul>
&lt;h3 id="33-逃逸分析的命令">3.3 逃逸分析的命令&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>你可以用如下命令查看逃逸分析结果：&lt;/p></description></item></channel></rss>