函數(shù)作參

實(shí)現(xiàn)類似python中裝飾器的函數(shù)

package main

import (
    "fmt"
)

func main(){
     //可以當(dāng)成類型復(fù)制
    ss := outsay("fucking golang",ssay)
    ss("yangtuo")
}

func ssay(s string){
    fmt.Println("lets go say: ", s)
}

func outsay(s string,f func( string)) (func(string)){  //有點(diǎn)像python裝飾器
    fmt.Println("this is outside code for add on say function")
    fmt.Println("handle 參數(shù),",s)
    return f
}

type typenam func(...)...用法例子

package main

import "fmt"

func main() {
    chr := 'K'
    chr -= ('A' - 'a')      //大寫換小寫
    fmt.Println(string(chr))

    t := File{
        name: "wanlb",
    }
    if t.testfunc == nil{
        fmt.Println("t testfunc is null")
        t.testfunc = testfunc(hello)
        t.testfunc("qiushu")
    }

}

type testfunc func(name string) string

func (t testfunc) say(){
    fmt.Println("test")
}

type File struct {
    name string
     testfunc
}

func hello(name string) string{
    fmt.Println("this is for test typefunc",name)
    return "ok test done"
}

testfunc 類理解:像是一類結(jié)合(funcname func(name string) string),都可以將函數(shù)類換成具體有函數(shù)內(nèi)容的函數(shù),進(jìn)行調(diào)用;跟interface{}有點(diǎn)像,空接口可以轉(zhuǎn)成任何類一樣,空接口也是類的集合!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 〇、前言 本文共108張圖,流量黨請慎重! 歷時(shí)1個(gè)半月,我把自己學(xué)習(xí)Python基礎(chǔ)知識(shí)的框架詳細(xì)梳理了一遍。 ...
    Raxxie閱讀 19,591評論 17 410
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    小邁克閱讀 3,127評論 1 3
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,842評論 6 427
  • 有很多和氣的情侶,有很少和氣的夫妻。 以孩子的出生為分水嶺。 那些聲稱從未吵過架的夫妻,真不知道該送你們什么表情包...
    采籮閱讀 332評論 0 3
  • 關(guān)掉朋友圈的日子,距今差不多1個(gè)月了。 1月27日那天,看見離職群里面,前同事的博文中,寫到其關(guān)閉朋友圈的感受,很...
    楊蓉Dorothy閱讀 470評論 2 1

友情鏈接更多精彩內(nèi)容