• 欢迎来到小爱博客,一个分享互联网IT技术的网站,喜欢就收藏吧!

GoLang条件语句&&循环语句

go 小爱 1个月前 (09-20) 22次浏览 已收录 0个评论 扫描二维码

条件语句

条件语言和分支是一个高级编程语言必须具备的特性,go语言中也提供了if和switch两种分支条件语句

if

语法

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">getSex</span><span class="token punctuation">(</span>sex <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token keyword">if</span> sex<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"男"</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> sex <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"女"</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"输入错误"</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

if 语句也可以将一个表达式放在判断条件中,并且表达式中的变量作用范围只是在此if语句中生效

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">getMax</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">if</span> a <span class="token operator">=</span> a <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> a <span class="token operator">></span> b <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"b"</span><span class="token punctuation">,</span>b<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

switch

switch 是处理多个条件或者分支的更加简洁的语法糖,go语言中提供了两种switch 语法

普通的switch

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">displaySwitch</span><span class="token punctuation">(</span>month <span class="token builtin">int</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
	<span class="token keyword">switch</span> month <span class="token punctuation">{</span>
	<span class="token keyword">case</span> <span class="token number">1</span><span class="token punctuation">:</span>
		fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"星期一"</span><span class="token punctuation">)</span>
	<span class="token keyword">case</span> <span class="token number">2</span><span class="token punctuation">:</span>
		fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"星期二"</span><span class="token punctuation">)</span>
	<span class="token keyword">default</span><span class="token punctuation">:</span>
		fmt<span class="token punctuation">.</span><span class="token function">Print</span><span class="token punctuation">(</span><span class="token string">"请输入正确"</span><span class="token punctuation">)</span>

	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

这种switch语法糖几乎每种语言都有,如果涉及到区间,这样做就很麻烦了,所以go语言有了带条件的switch语句

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">getGrage</span><span class="token punctuation">(</span>grage <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	<span class="token keyword">switch</span> <span class="token boolean">true</span> <span class="token punctuation">{</span>
	<span class="token keyword">case</span> grage <span class="token operator"><</span> <span class="token number">60</span><span class="token punctuation">:</span>
		<span class="token keyword">return</span> <span class="token string">"不及格"</span>
	<span class="token keyword">case</span> grage <span class="token operator">></span> <span class="token number">60</span> <span class="token operator">&&</span> grage <span class="token operator"><=</span> <span class="token number">70</span><span class="token punctuation">:</span>
		<span class="token keyword">return</span> <span class="token string">"良"</span>
	<span class="token keyword">case</span> grage <span class="token operator">==</span> <span class="token number">100</span><span class="token punctuation">:</span>
		<span class="token keyword">return</span> <span class="token string">"满分"</span>
	<span class="token keyword">default</span><span class="token punctuation">:</span>
		<span class="token function">panic</span><span class="token punctuation">(</span><span class="token string">"不合法"</span><span class="token punctuation">)</span>

	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

循环语句

go语言中提供了一种语法糖的循环关键字:for

n以内的和:

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">sum</span><span class="token punctuation">(</span>num <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	sum <span class="token operator">:=</span> <span class="token number">0</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> num<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		sum <span class="token operator">+=</span> i
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
</code>

将整数转换为2进制
可以没有开始条件

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">convertToBin</span><span class="token punctuation">(</span>num <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	result <span class="token operator">:=</span> <span class="token string">""</span>
	<span class="token keyword">for</span> <span class="token punctuation">;</span> num <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">;</span> num <span class="token operator">/=</span> <span class="token number">2</span> <span class="token punctuation">{</span>
		lsb <span class="token operator">:=</span> num <span class="token operator">%</span> <span class="token number">2</span>
		result <span class="token operator">=</span> strconv<span class="token punctuation">.</span><span class="token function">Itoa</span><span class="token punctuation">(</span>lsb<span class="token punctuation">)</span> <span class="token operator">+</span> result
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> result
<span class="token punctuation">}</span>
</code>

根据文件名从文件中读出数据并打印
可以没有开始和结束条件,其实相当于while(){}…

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">printFile</span><span class="token punctuation">(</span>fileName <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	file<span class="token punctuation">,</span> e <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span>
	<span class="token keyword">if</span> e <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	scanner <span class="token operator">:=</span> bufio<span class="token punctuation">.</span><span class="token function">NewScanner</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span>
	<span class="token keyword">for</span> scanner<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>scanner<span class="token punctuation">.</span><span class="token function">Text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

死循环

<code class="prism language-go has-numbering"><span class="token keyword">func</span> <span class="token function">forever</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

OR

<code class="prism language-go has-numbering">
<span class="token keyword">func</span> <span class="token function">forever1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"你好"</span><span class="token punctuation">)</span>		
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code>

其实循环内部实现的底层都一样,只是为了不同的场景,各个语言对外提供的api语法糖表现不一样罢了,go语言就统一了这种 ”不一样“ ,用for代替所有的循环


小爱博客 , 版权所有
转载请注明原文链接:GoLang条件语句&&循环语句
喜欢 (0)
【你的支持, 我的动力】
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址