当前位置:网站首页>Common mistakes daily practice 01
Common mistakes daily practice 01
2022-06-30 06:01:00 【Flower master】
1、defer and panic Use
package main
import "fmt"
func main() {
// This question mainly tests defer and panic
//defer After the function is executed ,panic Print before execution , Therefore, the printing sequence
// After printing
// Printing
// Before printing
//panic: Departure is abnormal
defer_call()
}
func defer_call() {
defer func () {
fmt.Println(" Before printing ")
}()
defer func () {
fmt.Println(" Printing ")
}()
defer func () {
fmt.Println(" After printing ")
}()
panic(" Departure is abnormal ")
}
2、for range Key points of circulation
package main
import "fmt"
func main() {
// This is mainly about pointer reference
//for range A copy of each element is created during the loop
// therefore m[key] = &val It's all variables val The address of , So finally map The values of all elements in are variables val The address of , Because in the end val To be an assignment 3, All the outputs are 3.
wrong()
fmt.Println("---------------------------------")
right()
}
func wrong() {
slice := []int{0,1,2,3}
m := make(map[int]*int)
for k,v := range slice {
m[k] = &v
}
for k,v := range m {
fmt.Println(k,*v)
}
}
func right() {
slice := []int{0,1,2,3}
m := make(map[int]*int)
for k,v := range slice {
value := v
m[k] = &value
}
for k,v := range m {
fmt.Println(k,*v)
}
}
边栏推荐
- 880. 索引处的解码字符串
- Summary of redis learning notes (I)
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- Solitidy - fallback 回退函数 - 2种触发执行方式
- [GPU] basic operation of GPU (I)
- At the beginning of 2022, people who are ready to change jobs should pay attention to
- Finally someone can make the server so straightforward
- Detailed explanation of issues related to SSL certificate renewal
- Network basics
- SparseArray
猜你喜欢
从零开发 stylelint规则(插件)
The average salary of software testing in 2022 has been released. Have you been averaged?
MySQL高级SQL语句
86. separate linked list
MySQL事物
Xctf attack and defense world crypto advanced area
[GPU] basic operation
Intelligent deodorizer embedded development
Did you know that WPS can turn on eye protection mode?
Solidity - Security - reentrancy attack
随机推荐
Luogup2756 pilot pairing scheme problem (maximum flow)
CompletionService使用及原理(源码分析)
Use of tornado template
Simple use of qlistview of QT (including source code + comments)
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
583. 两个字符串的删除操作-动态规划
Promise knowledge points
English grammar_ Adjective / adverb Level 3 - superlative
09- [istio] istio service entry
VLAN access mode
Use of OpenCL thread algebra library viennacl
How does WPS cancel automatic numbering? Four options
MySQL index
Configure the user to log in to the device through telnet -- AAA local authentication
Prototype and prototype chain in JS
从零开发 stylelint规则(插件)
Sound net, debout dans le "sol" de l'IOT
token 过期后,如何自动续期?
MySQL数据库用户管理
Official win 10 image download