首页 > 其他分享 >Go - break

Go - break

时间:2024-02-21 09:11:20浏览次数:24  
标签:code label break switch statement Go loop

A break statement is commonly used to terminate the execution of a loop. When loops are used in conjunction with switch or select, developers frequently make the mistake of breaking the wrong statement.
Let’s take a look at the following example. We implement a switch inside a for loop. If the loop index has the value 2, we want to break the loop:

This code may look right at first glance; however, it doesn’t do what we expect. The break statement doesn’t terminate the for loop: it terminates the switch statement, instead. Hence, instead of iterating from 0 to 2, this code iterates from 0 to 4: 0 1 2 3 4.
One essential rule to keep in mind is that a break statement terminates the execution of the innermost for, switch, or select statement. In the previous example, it terminates the switch statement.
So how can we write code that breaks the loop instead of the switch statement? The most idiomatic way is to use a label:

Here, we associate the loop label with the for loop. Then, because we provide the loop label to the break statement, it breaks the loop, not the switch. Therefore, this new version will print 0 1 2, as we expected.

 

标签:code,label,break,switch,statement,Go,loop
From: https://www.cnblogs.com/zhangzhihui/p/18024440

相关文章

  • Go 中如何高效遍历目录?探索几种方法
    Go中如何高效遍历目录?探索几种方法原创波罗学码途漫漫2024-02-2108:01上海听全文图片嗨,大家好!我是波罗学。本文是系列文章Go技巧第十八篇,系列文章查看:Go语言技巧。如果对我的文章感兴趣,欢迎关注我的公众号: 目录遍历是一个很常见的操作,它的使用场景有如文件目录查......
  • Go 100 mistakes - #32: Ignoring the impact of using pointer elements in range lo
    Thissectionlooksataspecificmistakewhenusingarangeloopwithpointerelements.Ifwe’renotcautiousenough,itcanleadustoanissuewherewereferencethe wrongelements.Let’sexaminethisproblemandhowtofixit.Beforewebegin,let’scla......
  • 【Go-Lua】Golang嵌入Lua代码——gopher-lua
    嵌入式8篇文章0订阅订阅专栏Lua代码嵌入GolangGo版本:1.19首先是Go语言直接调用Lua程序,并打印,把环境跑通packagemainimportlua"github.com/yuin/gopher-lua"funcmain(){ L:=lua.NewState() deferL.Close() //go err:=L.DoString(`print("gogogo!")`) iferr!=n......
  • golang time包和日期函数
    获取当前时间 //获取当前时间对象 timeObj:=time.Now() /*获取当前日期语法一*/ //打印当前日期 fmt.Println(timeObj)//2024-02-2017:50:54.085353+0800CSTm=+0.000323093 //当前年 year:=timeObj.Year() //打印当月 month:=timeObj.Month() //......
  • go自定义了一个Code的错误代码类型
    第一次基于GoFrame框架开发项目,这是一个灵感来自PHPLaravel的Golang开发框架,使用之后其实自己并不是很喜欢,把一个开发语言的习惯直接迁移到另一个开发语言上,个人觉得并不是一个好主意,不过这次并不想讨论这个。同事之前的实践异常处理是每个框架都需要考虑的问题,GoFrame也有自己......
  • golang函数
    函数定义/*函数定义关键字funcfunc函数名(参数参数类型)函数返回值的类型*/funcgetInfo(namestring,ageint)string{ returnname}//函数返回多个返回值:则返回类型括号包裹(返回值类型,类型..),即时返回两个int,也需要(int,int)funcgetNum(xint,statusboo......
  • Go - range
    Arangeloopallowsiteratingoverdifferentdatastructures:StringArrayPointertoanarraySliceMapReceivingchannelIngeneral,rangeproducestwovaluesforeachdatastructure exceptareceivingchannel,forwhichitproducesasingleeleme......
  • C 语言实现对 Go-Back-N 协议的模拟
    协议设计~事件动作发送方从应用层收到数据检查发送窗口是否已满,若窗口未满,产生一个分组并发送,并存储对应分组;若窗口已满,将数据存入缓冲区发送方超时重传所有已发送但还未被确认的分组发送方收到ACK若收到连续的ACK,窗口向前滑动,发送新的分组,重启计时器......
  • ABAP:GOS上传和下载附件功能
     GOS附件上传常用的几种对象类型:采购订单:BUS2012销售订单:BUS2032DN交货单:LIKP销售发票:VBRK采购发票:BUS2081会计凭证:BUS6035可以在TOJTT表中查找相关对象类型*&---------------------------------------------------------------------**&ReportZTEST_GOS......
  • golang随机数源码分析及应用
    引言大家刚开始使用随机数的时候可能会这样写,但是他会产生一个问题,这是什么问题呢funcmain(){ fori:=0;i<10;i++{ rand.Seed(time.Now().Unix()) fmt.Println(rand.Intn(100)) }}发现打印出来的结果都是相同的让我们看看用代码分析为什么产生这个问题首......