首页 > 其他分享 >Golang基础-Switch

Golang基础-Switch

时间:2023-02-19 15:33:52浏览次数:29  
标签:case do 20 age 基础 switch Golang Switch something

  • 不需要手动break
  • default是找不到case时执行
  • 可以对多个case执行同样的操作
operatingSystem := "windows"

switch operatingSystem {
case "windows", "linux":
    // do something if the operating system is windows or linux
case "macos":
    // do something if the operating system is macos
default:
    // do something if the operating system is none of the above
}
  • 空switch根据case的真伪选择是否执行
age := 21

switch {
case age > 20 && age < 30:
    // do something if age is between 20 and 30
case age == 10:
    // do something if age is equal to 10
default:
    // do something else for every other case
}
  • fallthrough语句实现case继续向下,就像C的switch不写break
age := 21

switch {
case age > 20:
    // do something if age is greater than 20
    fallthrough
case age > 30:
    // Since the previous case uses 'fallthrough',
    // this code will now run if age is also greater than 30
default:
    // do something else for every other case
}

标签:case,do,20,age,基础,switch,Golang,Switch,something
From: https://www.cnblogs.com/roadwide/p/17134825.html

相关文章

  • golang for 循环
    1.for循环for循环是Golang唯一的循环语句。for初始表达式;布尔表达式;迭代因子{循环体;}packagemainimport"fmt"funcmain(){ fori:=0;i<5;i++{......
  • TS基础知识点
    前言:TS简介相关介绍就不一一赘述,网上自行按照需求搜索查阅即可1.TypeScript的静态类型TypeScript的一个最主要特点就是可以定义静态类型,英文是StaticTyping。那到底......
  • golang 单测运行单个函数、文件、跳过文件命令
    1、单测运行1.2运行某个单测函数gotest-v-run=xxx,xxx是函数名,支持正则表达式;参数-v说明需要打印详情提示Golang单测是根据前缀匹配来执行的,gotest-v-run=......
  • K8S的基础概念
    一、Kubernetes介绍1、什么是Kubernetes?Kubernetes(通常称为K8s,K8s是将8个字母“ubernete”替换为“8”的缩写)是一个以容器为中心的基础架构,可以实现在物理集群或虚拟......
  • Linux基础 - 进程管理 ps
     psaux|sort-k4,4nr|head-n10 #查看内存占用前10名的程序ps-e/ps-ef/ps-eF/ps-ely  #Toseeeveryprocessonthesystemusingstandardsy......
  • 文件上传基础小总结
    基础及过滤方式什么是文件上传漏洞?存在文件上传的地方均有可能有文件漏洞(但不是一定有);如果上传代码某个地方存在验证疏忽,则会有文件漏洞危害可能直接获取网站权限,危......
  • Linux基础 - 文件系统 /proc
      一、/proc文件系统1.1/proc:一个虚拟文件系统/proc文件系统是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做/proc)。最初的设计目的是允许......
  • Java基础知识点(带返回值方法的定义和调用
    一:带返回值方法的定义方法的返回值其实就是方法运行的最终结果。如果要在调用处根据方法的结果,去编写另外一段逻辑,为了在调用处拿到方法的结果,就需要定义带返回值的方法。eg......
  • Fastjson2基础使用以及底层序列化/反序列化实现探究
    1Fastjson2简介Fastjson2是Fastjson的升级版,特征:协议支持:支持JSON/JSONB两种协议部分解析:可以使用JSONPath进行部分解析获取需要的值语言支持:Java/Kotlin场景支持:An......
  • Go基础-下
    一、面向对象:抽象:把一类事物的共有属性(字段)和行为(方法)抽取出来,形成一个物理模型(模板),这种研究问题的方法称为抽象1、面向对象的三大特性:继承、封装和多态封装:就是把抽......