首页 > 其他分享 >[Go] switch - fallthrough

[Go] switch - fallthrough

时间:2022-09-04 16:34:48浏览次数:57  
标签:case less fallthrough fmt Println switch Go than

fallthrough keyword is used in switch statement in golang. This keyword is used in switch case block. If the fallthrough keyword is present in the case block, then it will transfer control to the next case even though the current case might have matched.

package main

import "fmt"

func main() {
    i := 45

    switch {
    case i < 10:
        fmt.Println("i is less than 10")
    case i < 50:
        fmt.Println("i is less than 50")
    case i < 100:
        fmt.Println("i is less than 100")
    }
}

 

Output:

i is less than 50

 

By default the switch statement matches goes through all the case statement from top to bottom and tries to find the first case expression that matches the switch expression. Once the matching case is found, it exits  and does not consider the other cases. This is what is happening in above example. Even though i is less than 100 but that case is never executed because the second case is matched and after that it exits

fallthrough keyword allows way around this limitation. See below code for fallthrough keyword example. In below example even though the second case matched it went through the third case because of fallthrough keyword

ackage main

import "fmt"

func main() {
    i := 45
    switch {
    case i < 10:
        fmt.Println("i is less than 10")
        fallthrough
    case i < 50:
        fmt.Println("i is less than 50")
        fallthrough
    case i < 100:
        fmt.Println("i is less than 100")
    }
}

 

Output:

i is less than 50
i is less than 100

 

fallthrough needs to be final statement within the switch block. If it is not then compiler raise error

fallthrough statement out of place
package main

import "fmt"

func main() {
    i := 45
    switch {
    case i < 10:
        fmt.Println("i is less than 10")
        fallthrough
    case i < 50:
        fmt.Println("i is less than 50")
        fallthrough
        fmt.Println("Not allowed") // ERROR
    case i < 100:
        fmt.Println("i is less than 100")
    }
}

error as we have fmt.Println after the fallthrough statement

 

[Notice] be careful when use fallthrough, it seems that fallthrough will enter next case block no matter the condition. That might cause suprise bug in code;

func main() {
	i := 45
	switch {
	case i < 10:
		fmt.Println("i is less than 10")
		fallthrough
	case i < 50:
		fmt.Println("i is less than 50")
		fallthrough
	case i < 20:
		fmt.Println("i is less than 20")
	}
}

// i is less than 50
// i is less than 20

 

标签:case,less,fallthrough,fmt,Println,switch,Go,than
From: https://www.cnblogs.com/Answer1215/p/16655347.html

相关文章

  • [Go] Variables - non-declaration statement
    packagemainimport( "fmt")funcmain(){varfirstNamestring="firstName"varlastName="lastName"varfullNamestring//Gowillassignd......
  • [Go] Types - Reflect.TypeOf()
    Canusereflect.TypeOftogetvariabletypepackagemainimport( "fmt" "reflect")funcmain(){ //varageint=21 //varbbool=age>=23 varage......
  • shopee golang开发一面
    1、有环链表一个有环的链表,如何确认链表有环,环的长度。2、hashmap设计一个hashmap,你要用什么数据结构能用数组能做为存储hashmap的数据结构吗?不能的话,要怎么做?怎么解决......
  • golang面试经验2
    1、make和new的区别? 宝典导航2、了解过golang的内存管理吗?宝典导航3、调用函数传入结构体时,应该传值还是指针?说出你的理由?宝典导航4、线程有几种模型?宝典导航5、Go......
  • golang面试题2
    1、go方法值接收者和指针接收者结论:如果方法的接收者是指针类型,无论调用者是对象还是对象指针,修改的都是对象本身,会影响调用者      如果方法的接收者是值类......
  • 将 Go 类型打印为 S-Expressions
    将Go类型打印为S-ExpressionsPhotoby西格蒙德on不飞溅如果您已经使用Go一段时间,您可能知道当您使用“fmt”包渲染到控制台时,您可以从go结构中获得合理的输......
  • Could not find artifact org.pentaho:pentaho-aggdesigner-algorithm:pom:5.1.5-jhyd
    在Spark连接Hive导入相关maven依赖时<dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>2.3.4</version></de......
  • raise ConfigurationError(self._incompatible_err) pymongo.errors.ConfigurationErr
    python3ErrorFile"/home/xxx/.local/lib/python3.10/site-packages/pymongo/topology_description.py",line155,incheck_compatibleraiseConfigurationErr......
  • Django 与 Vue 语法冲突问题完美解决方法
    Django与Vue语法冲突问题完美解决方法当我们在djangoweb框架中,使用vue的时候,会遇到语法冲突.因为vue使用{{}},而django也使用{{}},因此会冲突.......
  • 使用 Django 集成 vue 到一个服务器上,还是 Django 和 vue 分开部署
    Django+Vue的项目,实际部署的时候,使用Django集成vue到一个服务器上,还是Django和vue分开部署?目前在架构选择,基本上定了Django+Vue但是实际部署的时候,就有两种......