Golang中defer、閉包以及命名傳回值

來源:互聯網
上載者:User

defer 後面跟普通函數

package mainfunc main() {    run()}func run() {    name := "Paul"    defer sayHello(name)     name = "John"}func sayHello(name string) {    println("Hello " + name)}// Hello Paul 只是延緩執行,實參會按照正常順序傳入函數中

defer、return 和未命名的傳回值

package mainfunc main() {    name := run()    println("return: name = " + name) }func run() (string) {    name := "Paul"    defer sayHello(&name)    name = "John"    return name}func sayHello(name *string) {    *name = "George"    println("Hello " + *name)}//Hello John  雖然實參提前傳入,但是傳入的是一個指標,最後執行拿到的是經過修改過的值//Hello George//return: name = John  未命名傳回值,所以 defer 無法對返回的值進行操作的

defer、return 和命名的傳回值

package mainfunc main() {    name := run()    println("return: name = " + name) func run() (x string) {    name := "Paul"    x = name    defer sayHello(&x)     name = "John"    return name}func sayHello(name *string) {    println("Hello " + *name)    *name = "George"    println("Hello " + *name)}//Hello John//Hello George//return: name = George 因為使用命名傳回值,所 defer 裡面依然可以對 defer 進行操作
package mainfunc main() {    name := run()    println("return: name = " + name) func run() (x string) {    name := "Paul"    x = name    defer sayHello(x)    name = "John"    return name}func sayHello(name string) {    println("Hello " + name)    name = "George"    println("Hello " + name)}//Hello Paul  順延強制但是按照程式順序執行,傳入的實參為 Paul //Hello George 函數內修改對外面的值沒有影響//return: name = John 

defer 閉包函數

package mainfunc main() {    name := run()    println("return: name = " + name)}func run() (string) {    name := "Paul"    aFun := func() {        println("Hello " + name)        name = "George"        println("Hello " + name)    }    name = "John"    aFun()    return name}//Hello John 雖然閉包定義時,外部變數 name = "Paul", 但是其實只是記錄是一個指標,所以最後執行的時候 name = "John" //Hello George//return: name = George
package mainfunc main() {    name := run()    println("return: name = " + name)}func run() (x string) {    name := "Paul"    aFun := func() {        println("Hello " + x)        x = "George"        println("Hello " + x)    }    defer aFun()    name = "John"    return name}//Hello John //Hello George//return: name = George  defer 對傳回值進行了修改
package mainfunc main() {    name := run()    println("return: name = " + name)}func run() (x string) {    name := "Paul"    x = name    aFun := func(x string) {        println("Hello " + x)        x = "George"        println("Hello " + x)    }    defer aFun(x)    name = "John"    return name}//Hello Paul  實參按照順序拷貝傳入 x = "Paul"//Hello George //return: name = John  因為設定了形參所以閉包內的對應的變數只是在局部生效,不會影響外部變數
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.