杂记5

1.switch

  • switch后带表达式时,switch-case只能模拟相等的情况;如果不带表达式(即 空switch),case后就可以跟任意的条件表达式,例:
switch {
  case add(5) > 10:
       fmt.Println("right")
  default:        
       fmt.Println("wrong")
}
  • switch Type (.(type)只能用在switch内部!!)

var num interface{} = 6.5 switch value := num.(type) { //相当于在每个case内部申明了一个变量value(看下例可知) case int: //value已被转换为int类型 fmt.Printf("number is int %d\n", value) case float32: //value已被转换为float32类型 fmt.Printf("number is float32 %f\n", value) case byte, string: //如果case后有多个类型,则value还是interface{}类型 fmt.Printf("number is interface %v\n", value) default: fmt.Printf("number is unknown type") } 下例:

switch num.(type) { 
case int: 
    value := num.(int) 
    fmt.Printf("number is int %d\n", value) //value已被转换为int类型
case float32: 
    value := num.(float32)
    fmt.Printf("number is float32 %f\n", value)
case byte: 
    value := num.(byte)
    fmt.Printf("number is byte %d\n", value)
default:
    fmt.Printf("number is unknown type")
}

 

2.fallthrough

  • 在switch type语句的 case 子句中不能使用fallthrough
  • 每一个case后面都要加fallthrough,因为它只会延长判断一个
func fall(age int) {
  switch {
     case age > 50:
         fmt.Println("5a hhh")  
         fallthrough
     case age > 30:
         fmt.Println("3b hhh")  
         fallthrough
     case age > 10:
         fmt.Println("1c hhh")  
         fallthrough
     default:
         ...
  }  
}

还可以这样:

func fall(age int) {
  switch {
     case age > 50:   // 大于50只会执行这一种情况
         fmt.Println("5a hhh")  
     case age > 30:   // 大于30会执行 >30 和 >10 的情况 及 default下面的情况
         fmt.Println("3b hhh")  
         fallthrough
     case age > 10:
         fmt.Println("1c hhh")  
         fallthrough
     default:
         ...
  }  
}

 

3.interface可实现函数重载

func square1(a float32) float32 {
   return a * a
}

func square2(a int) int {
   return a * a
}

func square3(a byte) byte {
   return a * a
}

//像上述3个函数就可以统一写为下面这一个函数
func square(a interface{}) interface{} { switch value := a.(type) { case int: return value * value case byte: return value * value case float32: return value * value default: return nil } }

interface{}类型不能直接运算,要先强制转换,因为可能它表示的是一个复杂的变量不能直接运算

 

4.for range拿到的是数据的拷贝

 

5.二维切片数组A。A[:][1] <==> A[1] : 数组第二行A[0:2][1] : 截取数组前两行,取其中的第二行

 

6.goto与Lable(这个很像汇编的 循环 或者 跳转到某处代码用法)

  • Lable定义后必须在代码的某个地方被使用
  • 除非遇见新的Lable,否则Lable下的内容都属于它
var i int = 4 
MY_LABEL:
  i += 3
  fmt.Println(i)
  goto MY_LABEL//返回定义MY_LABEL的那一行,把代码再执行一遍(会进入一个无限循环)

 

func if_goto() {
  var i int = 4
  if i%2 == 0 { 
        goto L1 //先使用Label
  } else {
        goto L2
  }

  L1://后定义Label
    i += 3

  L2: //Label定义后必须在代码的某个地方被使用
    i *= 3
    fmt.Println(i)
}

//因为用的是if判断,只判断一次,并且if条件后花括号里只有1行语句,所以执行完L1也不需要返回去花括号里继续执行,所以程序执行完L1就会继续往下执行L2
//所以结果是21,如果goto L1后接了return,i的值就不是21,而是7

 

//退出for循环

//goto与Label结合可以实现break的功能,甚至比break更强 
for i := 0; i < SIZE; i++ { 
  L2:
     for j:= 0; j< SIZE; j++ {
       goto L1
     }
}

L1:
 XXX

 

7.break、continue 与 Lable

lable里面可以用break和continue,并且可以提升两者功能,比如如果lable在第一层循环外面,break或者continue在第三层循环里面,那么break或者continue可以直接退出或者跳出第一层。不然原先是只能跳出一层(即第二层的)

  • break、continue与Lable 结合使用可以跳转到更外层的for循环
  • continue 和 break 针对的Lable必须写在for前面,而goto可以针对任意位置的Lable

语句:break L1; continue L1

 

posted @ 2023-01-27 16:55  balabalahhh  阅读(20)  评论(0)    收藏  举报