The question about defer

fun f6() int {
    var i int = 0
    defer func(a int) {
        fmt.Println("i f6:", a)
        i = i+5
    }(i)
    i = 1
    iPP
    return i
}

according to the rewriting rule, the return statement is separated into two sentences., return xxx will be rewritten as:
return value = xxx
call defer function
empty return

iTun0
return value r = i (i equals 0)
iTun1
iPP (i equals 2)
return r (I think it should be 0)

Why is the result 2?

Mar.09,2021

I don't understand go, but take a look at the defer function
. Isn't that how it works

?
fun f6() int {
    var i int = 0
    i = 1
    iPP
    // return xxx
    // defer  RET
    r = i
    func(a int) {
         fmt.Println("i f6:", a)
         i = i+5
    }(i)
    return
}

so

return I is a single instruction multiple operation is
in the return I code is changed to r = I; return,
but the defer function will be between assignment and return, and your function does not affect the value of the external scope I
so the return must be 2, not 0.
certainly does not change I to r in advance, which is illogical

.

I am also a beginner of go. This is how I understand

.

`

func f6() int {  // fun ==> func 
    var i int = 0
    defer func(a int) {  //
        fmt.Println("i f6:", a)   // a = i = 0 print 0
        i = i+5
    }(i)
    i = 1     
    iPP   // i = 2
    return i  // return 2
}

`

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e3c3eb-43615.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e3c3eb-43615.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?