教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go语言函数参数与返回值示例,数组的简单应用

go语言函数参数与返回值示例,数组的简单应用

发布时间:2022-03-26   编辑:jiaochengji.com
教程集为您提供go语言函数参数与返回值示例,数组的简单应用等资源,欢迎您收藏本站,我们将为您提供最新的go语言函数参数与返回值示例,数组的简单应用资源
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"/></svg>

go语言函数参数与返回值示例,数组的简单应用

<pre class="prettyprint"><code class=" hljs go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> <span class="hljs-string">"fmt"</span> <span class="hljs-keyword">func</span> main() { string1 ,string2 :=test_various_values<span class="hljs-number">(30</span><span class="hljs-number">,58</span>) fmt.Println(<span class="hljs-string">"参数1 "</span>,string1) fmt.Println(<span class="hljs-string">"参数2 "</span>,string2) fmt.Println(<span class="hljs-string">"------------------------------------------------------------------"</span>) v1 ,v2 :=test_various_values2<span class="hljs-number">(46</span>,<span class="hljs-string">"huangmao"</span>) fmt.Println(<span class="hljs-string">"--v1==== "</span>,v1) fmt.Println(<span class="hljs-string">"---v2 == "</span>,v2) fmt.Println(<span class="hljs-string">"------------------------数组遍历----------------------------------"</span>) declarateType() } <span class="hljs-comment">/** *测试多个输入返回值函数 *arg1,arg2 为传入参数,int 为传入参数指定类型; *str1,str2 为返回值参数。 * */</span> <span class="hljs-keyword">func</span> test_various_values(arg1,arg2 <span class="hljs-typename">int</span>)(str1 <span class="hljs-typename">string</span>,str2 <span class="hljs-typename">string</span>){ fmt.Println(<span class="hljs-string">"a==="</span>,arg1) fmt.Println(<span class="hljs-string">"b==="</span>,arg2) fmt.Println(<span class="hljs-string">"a b="</span>,arg1 arg2) str1=<span class="hljs-string">"hell"</span> str2=<span class="hljs-string">"world"</span> <span class="hljs-keyword">var</span> mun <span class="hljs-typename">int</span> mun <span class="hljs-number">=34</span> fmt.Println(<span class="hljs-string">"num原始值=="</span>,mun) mun=arg1 fmt.Println(<span class="hljs-string">"num复制结果=="</span>,mun) <span class="hljs-keyword">return</span> str1,str2 } <span class="hljs-keyword">func</span> test_various_values2(arg1 <span class="hljs-typename">int</span>,arg2 <span class="hljs-typename">string</span> )(reslut1 <span class="hljs-typename">int</span>,reslut2 <span class="hljs-typename">string</span>){ <span class="hljs-keyword">var</span> num <span class="hljs-typename">int</span> num=arg1 fmt.Println(<span class="hljs-string">"参数1====test_various_values2===="</span>,num) <span class="hljs-keyword">var</span> v1 <span class="hljs-typename">string</span> v1=arg2 fmt.Println(<span class="hljs-string">"参数2=====test_various_values2=="</span>,v1) reslut1=num reslut2=v1 <span class="hljs-keyword">return</span> reslut1 ,reslut2 } <span class="hljs-comment">/** *定义变量 * */</span> <span class="hljs-keyword">func</span> declarateType( ){ <span class="hljs-keyword">var</span> z_num <span class="hljs-typename">int</span> z_num<span class="hljs-number">=45</span> fmt.Println(z_num) <span class="hljs-keyword">var</span> str1 <span class="hljs-typename">string</span> str1=<span class="hljs-string">"hello go"</span> fmt.Println(str1) <span class="hljs-comment">//定义数组</span> <span class="hljs-keyword">var</span> myArray <span class="hljs-number">[10</span>] <span class="hljs-typename">int</span> myArray= <span class="hljs-number">[10</span>] <span class="hljs-typename">int</span><span class="hljs-number">{1</span>,<span class="hljs-number"> 2</span>,<span class="hljs-number"> 3</span>,<span class="hljs-number"> 4</span>,<span class="hljs-number"> 5</span>,<span class="hljs-number"> 6</span>,<span class="hljs-number"> 7</span>,<span class="hljs-number"> 8</span>,<span class="hljs-number"> 9</span>,<span class="hljs-number"> 10</span>} <span class="hljs-keyword">for</span> i :=<span class="hljs-number"> 0</span>; i <<span class="hljs-built_in">len</span>(myArray); i { fmt.Println(<span class="hljs-string">"输出数组值===myArray===="</span>,i,<span class="hljs-string">"===="</span>,myArray[i]) } <span class="hljs-comment">//基于myArray的前5个元素创建数组切片:</span> <span class="hljs-keyword">var</span> mySlice = myArray[<span class="hljs-number">:5</span>] <span class="hljs-keyword">for</span> i :=<span class="hljs-number"> 0</span>; i < <span class="hljs-built_in">len</span>(mySlice); i { fmt.Println(<span class="hljs-string">"输出数组值==mySlice==="</span>,i,<span class="hljs-string">"===="</span>,mySlice[i]) } <span class="hljs-keyword">var</span> mybehind=myArray<span class="hljs-number">[5</span>:] <span class="hljs-keyword">for</span> i :=<span class="hljs-number"> 0</span>; i <<span class="hljs-built_in">len</span>(mybehind); i { fmt.Println(<span class="hljs-string">"输出数组值==mybehind==="</span>,i,<span class="hljs-string">"===="</span>,mybehind[i]) } }</code></pre>

相关数组介绍使用参见:http://studygolang.com/articles/2894

到此这篇关于“go语言函数参数与返回值示例,数组的简单应用”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Go 语言向函数传递数组
Go 语言数组
Go 语言函数
Go 语言指针
Go 语言多维数组
Go 语言函数作为值
Go 语言范围(Range)
Go 语言结构
Go 语言函数闭包
Go 语言教程

[关闭]
~ ~