企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 第1.3函数 - 函数定义,支持多返回值,返回值初始化0, ``` <pre class="calibre16">``` func <span class="token1">hypot</span><span class="token2">(</span>x<span class="token2">,</span> y float64<span class="token2">)</span> float64 <span class="token2">{</span> <span class="token5">return</span> math<span class="token2">.</span><span class="token1">Sqrt</span><span class="token2">(</span>x<span class="token">*</span>x <span class="token">+</span> y<span class="token">*</span>y<span class="token2">)</span> <span class="token2">}</span> func <span class="token1">typedTwoValues</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">(</span>int<span class="token2">,</span> int<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span> <span class="token2">}</span> 匿名函数<span class="token2">,</span>传递后可以调用 f <span class="token2">:</span><span class="token">=</span> <span class="token1">func</span><span class="token2">(</span>data int<span class="token2">)</span> <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span><span class="token4">"hello"</span><span class="token2">,</span> data<span class="token2">)</span> <span class="token2">}</span> 可变参数 func <span class="token1">myfunc</span><span class="token2">(</span>args <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span>int<span class="token2">)</span> <span class="token2">{</span> <span class="token5">for</span> _<span class="token2">,</span> arg <span class="token2">:</span><span class="token">=</span> range args <span class="token2">{</span> fmt<span class="token2">.</span><span class="token1">Println</span><span class="token2">(</span>arg<span class="token2">)</span> <span class="token2">}</span> <span class="token2">}</span> 递归阶乘 func <span class="token1">Factorial</span><span class="token2">(</span>n uint64<span class="token2">)</span> <span class="token2">(</span>result uint64<span class="token2">)</span> <span class="token2">{</span> <span class="token5">if</span> n <span class="token">></span> <span class="token3">0</span> <span class="token2">{</span> result <span class="token">=</span> n <span class="token">*</span> <span class="token1">Factorial</span><span class="token2">(</span>n<span class="token">-</span><span class="token3">1</span><span class="token2">)</span> <span class="token5">return</span> result <span class="token2">}</span> <span class="token5">return</span> <span class="token3">1</span> <span class="token2">}</span> ``` ```