1 conn, err := websocket.Upgrade(w, req, w.Header(), bufSize, bufSize) 2 if err != nil { 3 code = errWSUpgradeFailed 4 msg = err.Error() 5 return 6 } 7 defer conn.Close()
defer conn.Close() 要放在判err之后,因为err时conn为nil,然后nil.Close()会使程序出错,甚至直接panic崩调
code := Success msg := "" var response responseJSON defer func() { writeResponse(msg, response, w, code) }()
defer执行匿名函数
1 package main 2 import "fmt" 3 func f1() (result int) { 4 println("-------------------------------") 5 defer func() { 6 result++ 7 }() 8 return 0 9 } 10 func f2() (r int) { 11 println("-------------------------------") 12 t := 5 13 defer func() { 14 t = t + 5 15 }() 16 return t 17 } 18 func f3() (r int) { 19 println("-------------------------------") 20 defer func(r int) { 21 r = r + 5 22 }(r) 23 return 1 24 } 25 func f4() (r int) { 26 println("-------------------------------") 27 r=100 28 return 29 } 30 func f5() (r1 int,r2 interface{},r3 string) { 31 println("-------------------------------") 32 return 33 } 34 func f6() (r1 int) { 35 println("-------------------------------") 36 defer func() { 37 if r1==100 { 38 println("r1==100") 39 } 40 }() 41 r1=100 42 return 43 } 44 func f7() (r1 int) { 45 println("-------------------------------") 46 r1=100 47 return 1 48 } 49 func f8() (r1 int) { 50 println("-------------------------------") 51 r1=100 52 defer func() { 53 r1++; 54 }() 55 return 1 56 } 57 func f9() (r1 int) { 58 println("-------------------------------") 59 defer func(r int) { 60 r++ 61 }(r1) 62 return 63 } 64 func f10() (r interface{}) { 65 println("-------------------------------") 66 r=1 67 defer func(a interface{}) { 68 fmt.Println("a:",a) 69 fmt.Println("r:",r) 70 a=100 71 fmt.Println("a:",a) 72 fmt.Println("r:",r) 73 }(r) 74 return 75 } 76 func f11() (r interface{}) { 77 println("-------------------------------") 78 r=1 79 defer func(a interface{}) { 80 fmt.Println("a:",a) 81 fmt.Println("r:",r) 82 a=100 83 fmt.Println("a:",a) 84 fmt.Println("r:",r) 85 }(r) 86 r=2 87 defer func(a interface{}) { 88 fmt.Println("second a:",a) 89 fmt.Println("second r:",r) 90 a=200 91 fmt.Println("second a:",a) 92 fmt.Println("second r:",r) 93 }(r) 94 return 95 } 96 func f12() (r interface{}) { 97 println("-------------------------------") 98 r=1 99 defer func(a interface{}) { 100 fmt.Println("a:",a) 101 fmt.Println("r:",r) 102 a=100 103 fmt.Println("a:",a) 104 fmt.Println("r:",r) 105 }(r) 106 r=2 107 defer func(a interface{}) { 108 fmt.Println("second a:",a) 109 fmt.Println("second r:",r) 110 a=200 111 fmt.Println("second a:",a) 112 fmt.Println("second r:",r) 113 }(r) 114 r=3 115 return 116 } 117 func f13() (a interface{},b interface{}) { 118 println("-------------------------------") 119 return 1,2 120 } 121 func f14() map[string]string{ 122 println("-------------------------------") 123 a:=map[string]string{"a":"b"} 124 defer func(mp map[string]string) { 125 mp["a"]="c" 126 }(a) 127 return a 128 } 129 func f15() string{ 130 println("-------------------------------") 131 a:="a" 132 defer func(mp string) { 133 mp="b" 134 }(a) 135 return a 136 } 137 func f16() []byte{ 138 println("-------------------------------") 139 a:=[]byte("a") 140 defer func(mp []byte) { 141 mp=[]byte("b") 142 }(a) 143 return a 144 } 145 func f17() chan int{ 146 println("-------------------------------") 147 a:=make(chan int,10) 148 defer func(mp chan int) { 149 mp<-1 150 }(a) 151 return a 152 } 153 func main() { 154 println(f1()) 155 println(f2()) 156 println(f3()) 157 println(f4()) 158 a,b,c:=f5() 159 fmt.Println(a,b,c) 160 println(f6()) 161 println(f7()) 162 println(f8()) 163 println(f9()) 164 fmt.Println(f10()) 165 fmt.Println(f11()) 166 fmt.Println(f12()) 167 e,f:=f13() 168 fmt.Println(e,f) 169 fmt.Println(f14()) 170 fmt.Println(f15()) 171 fmt.Println(string(f16())) 172 fmt.Println(len(f17())) 173 }
1 ------------------------------- 2 1 3 ------------------------------- 4 5 5 ------------------------------- 6 1 7 ------------------------------- 8 100 9 ------------------------------- 10 0 <nil> 11 ------------------------------- 12 r1==100 13 100 14 ------------------------------- 15 1 16 ------------------------------- 17 2 18 ------------------------------- 19 0 20 ------------------------------- 21 a: 1 22 r: 1 23 a: 100 24 r: 1 25 1 26 ------------------------------- 27 second a: 2 28 second r: 2 29 second a: 200 30 second r: 2 31 a: 1 32 r: 2 33 a: 100 34 r: 2 35 2 36 ------------------------------- 37 second a: 2 38 second r: 3 39 second a: 200 40 second r: 3 41 a: 1 42 r: 3 43 a: 100 44 r: 3 45 3 46 ------------------------------- 47 1 2 48 ------------------------------- 49 map[a:c] 50 ------------------------------- 51 a 52 ------------------------------- 53 a 54 ------------------------------- 55 1
defer会从下往上执行,若return时没到达的defer不执行,
非匿名函数
func(a interface{},b interface{}) (c interface{},d interfacd{}) {
c=3
d=4
return
return 1,2
}
c和d在进入函数时定义,一旦return,c和d的值都会返回,如果return 1,2,则1,2的值会赋给c,d,return时要么都不赋值,要么都赋值,不能只赋部分
对于匿名函数func(a interface{}) {
a=1
}(r)
表示将r传入函数作为a的值,除了chan和map,别的一定是复制传入(interface{},[]byte,etc),不会传入指针,而且a的值是运行函数时r的值