首页 > 其他分享 >03. defer和switch

03. defer和switch

时间:2024-10-09 13:59:54浏览次数:1  
标签:case 03 defer fmt Println switch Go 语句

1. switch

switch 语句是编写一连串 if - else 语句的简便方法。它运行第一个 case 值, 值等于条件表达式的子句。

Go 的 switch 语句类似于 C、C++、Java、JavaScript 和 PHP 中的,不过 Go 只会运行选定的 case,而非之后所有的 case。 在效果上,Go的做法相当于这些语言中为每个case后面自动添加了所需的break语句。在 Go 中,除非以fallthrough语句结束,否则分支会自动终止。 Go 的另一点重要的不同在于 switch 的 case 无需为常量,且取值不限于整数。

package main

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Print("Go 运行的系统环境:")
	switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("macOS.")
	case "linux":
		fmt.Println("Linux.")
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.\n", os)
	}
}

1.1 switch的求值顺序

switch 的 case 语句从上到下顺次执行,直到匹配成功时停止。

例如,

switch i {
case 0:
case f():
}

在 i==0 时,f 不会被调用。

无条件的 switch 同 switch true 一样。

这种形式能将一长串 if-then-else 写得更加清晰。

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("早上好!")
	case t.Hour() < 17:
		fmt.Println("下午好!")
	default:
		fmt.Println("晚上好!")
	}
}

标签:case,03,defer,fmt,Println,switch,Go,语句
From: https://www.cnblogs.com/yyyylllll/p/18454077

相关文章

  • why do we need 'select…for share' instead of a simple 'select'
    (Fromchatgpt)AsimpleSELECTqueryinPostgreSQLoperatesundertheMVCC(Multi-VersionConcurrencyControl)model,whichallowsittoreaddatawithoutlockingtherows.Thismeansitcanseeasnapshotofthedataatthestartofthetransaction,rega......
  • 学习011-08-03 Numeric Properties(数字属性)
    NumericProperties(数字属性)XAFsupportsPropertyEditorsfornumericdatatypes(byte,int,decimal,long,correspondingnullabletypes,etc.)onallplatforms.However,WinForms,ASP.NETWebForms,andBlazorUIapplicationsusedifferentformattingru......
  • 学习011-08-03-01 Numeric Properties in XPO(XPO中的数字属性)
    NumericPropertiesinXPO(XPO中的数字属性)TheexamplebelowillustrateshowtoimplementNumericPropertiesinanXPOpersistentclass.下面的示例说明了如何在XPO持久类中实现数字属性。C#privatedoubledoubleProperty;publicdoubleDoubleProperty{g......
  • 学习011-08-03-02 Numeric Properties in EF Core(EF Core中的数字属性)
    NumericPropertiesinEFCore(EFCore中的数字属性)TheexamplebelowillustrateshowtoimplementNumericPropertiesinanEFCoreclass.下面的示例说明了如何在EFCore类中实现数字属性。C#publicvirtualdoubleDoubleProperty{get;set;}publicvirtual......
  • nacos gateway 调用服务报错503 Server unavailable
    环境springboot3jdk17依赖<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframe......
  • Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to ma
    这个错误通常发生在使用SpringAOP时,尤其是当你尝试访问AopContext.currentProxy(),但当前代理对象不可用时。下面是一些解决此问题的建议:1.启用 exposeProxy 属性确保你的AOP配置中设置了exposeProxy属性为true。这可以在使用注解或XML配置中进行设置使用注解如......
  • 代码随想录算法训练营第三天|链表理论基础、203.移除链表元素、707.设计链表、206.反
    链表理论基础链表的类型单链表每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链表的入口节点称为链表的头结点也就是head。双链表单链表中的指针域只能指向节点的下一个节点。双链表:每一个节点有......
  • Hetao P1031 萌萌题 题解 [ 蓝 ] [ 线性 dp ]
    萌萌题:一道结合了观察性质的线性dp。观察我们先考虑极端情况:所有数相同,所有数降序排列两种情况。对于所有数相同的情况,我们发现,最终可以合并出来的区间,最多只有\(n\logn\)个。怎么证明?考虑固定右端点,那么我们想要合并出一个点,就得选\(2^k\)个数出来,这就有\(\logn\)......
  • 2018_11_02_03
    plugin插件注册importPickerComponentfrom'./picker.vue';let$vm;exportdefault{install(Vue,options){if(!$vm){constpickerPlugin=Vue.extend(PickerComponent);$vm=newpickerPlugin({el:document.createElement......
  • 'in fact'的用法和位置
    'infact'的用法和位置'infact'是一个介词短语,通常用于强调某个事实或观点,或者对前面提到的信息进行修正或补充。它可以在句子中作为状语使用,位于句首、句中或句末。'infact'的具体例子‌强调事实‌:Infact,IenjoyedthemoviemorethanIthoughtIwould.(事实上,我比预......