zoukankan      html  css  js  c++  java
  • go ---switch语句

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	var ar = [...]string{"A", "B", "D", "E"}
    
    	for _, content := range ar {
    		switch content {
    		case "A":
    			fmt.Println("AAA")
    		case "B", "C", "D":
    			fmt.Println("BBB")
    		default:
    			fmt.Println("CCC")
    		}
    
    	}
    
    }
    

      输出:

    AAA

    BBB

    BBB

    CCC

     使用fallthrough,来向下一个case语句转移流程控制权,

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	var ar = [...]string{"A", "B", "D", "E"}
    
    	for _, content := range ar {
    		switch content {
    		case "A":
    			fallthrough
    		case "B", "C", "D":
    			fmt.Println("BBB")
    		default:
    			fmt.Println("CCC")
    		}
    
    	}
    
    }
    

      输出:

    BBB

    BBB

    BBB

    CCC

    类型switch语句:对类型进行判定,而不是值

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	v := 11
    
    	switch i := interface{}(v).(type) {
    
    	case int, int8, int16, int32, int64:
    
    		fmt.Printf("A signed integer: %v. the type is :%T
    ", v, v)
    		fmt.Printf("A signed integer: %v. the type is :%T
    ", i, i)
    	case uint, uint8, uint16, uint32, uint64:
    
    		fmt.Printf("A unsigned integer: %v. the type is :%T
    ", v, v)
    		fmt.Printf("A unsigned integer: %v. the type is :%T
    ", i, i)
    
    	default:
    
    		fmt.Println("Unknown!")
    
    	}
    
    }
    

      输出:

    A signed integer: 11. the type is :int

    A signed integer: 11. the type is :int

    package main
    
    import (
    	"fmt"
    )
    
    func main() {
    
    	var v interface{}
    	v = "hi"
    
    	switch v.(type) {
    	case string:
    		fmt.Printf("The string is %v 
    ", v.(string))
    
    	case int, int8, int16, int32, int64:
    
    		fmt.Printf("A signed integer: %v. the type is :%T
    ", v, v)
    
    	case uint, uint8, uint16, uint32, uint64:
    
    		fmt.Printf("A unsigned integer: %v. the type is :%T
    ", v, v)
    
    	default:
    
    		fmt.Println("Unknown!")
    
    	}
    
    }
    

      输出:

    The string is hi 

  • 相关阅读:
    Python语法解析器PLY——lex and yacc in Python
    spider-lang :爬虫语言,专为网络爬虫设计
    使用ANTLR做一个简单的Python SQL语法解析器
    使用Antlr实现简单的DSL
    Wrights Notes
    20个人团建能干些什么?
    zz
    贾跃亭反思乐视节奏过快_公司频道_财新网
    西湖人才网 职称考评
    安能物流
  • 原文地址:https://www.cnblogs.com/saryli/p/11634472.html
Copyright © 2011-2022 走看看