V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
rix

一個簡單的函數式參數合併 helper

  •  
  •   rix · Sep 9, 2022 · 2073 views
    This topic created in 1327 days ago, the information mentioned may be changed or developed.

    Golang 常用的函數式參數範式最早由 Rob Pike 提出

    使用這種模式的時候我們經常要重複類似下面這段代碼:

    opts := new(optionsT)
    for _, f := range options {
        f(opts)
    }
    

    現在借助泛型,我寫了一個幾行代碼的 helper 來化簡:

    import "gopkg.in/option.v0"
    opts := option.New(options)
    

    當你有很多使用這個範式的函數的時候,這個 helper 用起來能省一些事情。 之前我是每個項目裏面都會直接存一份這個 helper ,但是後來發現用得太多了,就專門弄成一個庫可以直接 import 了。

    4 replies    2022-11-18 22:07:12 +08:00
    iamperiod331www
        1
    iamperiod331www  
       Sep 9, 2022
    Go 的语法快要改的不认识了
    YangWaleed
        2
    YangWaleed  
       Sep 9, 2022
    @iamperiod331www 比我好,我已经不认识了
    rekulas
        3
    rekulas  
       Sep 10, 2022
    其实挺好的,以前写代码总感觉束手束脚,现在加了泛型灵活多了
    czyt
        4
    czyt  
       Nov 18, 2022
    gopkg.in 链接怎么搞得
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5992 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:16 · PVG 10:16 · LAX 19:16 · JFK 22:16
    ♥ Do have faith in what you're doing.