dongzhao1930 2018-01-05 04:22
浏览 30
已采纳

有效地将公共SDK类型包装在Golang中

I am using pagerduty go sdk to do a bunch of api requests. Particularly I am making use of

func NewClient(authToken string) *Client

to create a new Client type. I want to add some utility functions to my own work to *Client. I tried doing this:

type BetterPdClient *pagerduty.Client

func NewClient(auth string) BetterPdClient {
    return pagerduty.NewClient(auth)

}

func (b *BetterPdClient) DoSomething() {
    b.GetIncident(....)
}

func main() {
    pd_client := NewClient("")
    fmt.Println(pd_client)
    pd_client.DoSomething()
}

But I get the following error:

invalid receiver type *BetterPdClient (BetterPdClient is a pointer type) 

I understand that DoSomething() is expecting a pointer as caller. Only other way I could think of is sending the ptr as a function argument:

func NewClient(auth string) *pagerduty.Client { 
    return pagerduty.NewClient(auth) 

} 

func DoSomething(cl *pagerduty.Client) { 
    fmt.Println(cl) 
} 

func main() { 
    pd_client := NewClient("") 
    fmt.Println(pd_client) 
    DoSomething(pd_client) 
} 

Is there a better way?

  • 写回答

1条回答 默认 最新

  • donglou1866 2018-01-05 06:57
    关注

    Declaring a type as a pointer to another type is almost never what you want because Go doesn't allow you to add methods to that new type, nor to the pointer of that type as you've already figured out yourself. This one doesn't compile either:

    type T struct{}
    
    type P *T
    
    func (P) M() {}
    

    If you want to "extend" a type without "hiding" it's existing functionality your best bet is to embed it in a struct.

    type T struct{
        // ...
    }
    
    func (T) M() {}
    
    type U struct {
        *T
    }
    
    func NewU() *U {
        return &U{&T{}}
    }
    
    func (U) N() {}
    
    func main() {
        u := NewU()
    
        u.M()
        u.N()
    }
    

    And what I mean by "hiding existing functionality" is that when you define a new type in terms of another, already existing type, your new type will not get direct access to the methods of the existing type. All you're doing is just saying that your new type should have the same structure as the already existing type. Although it's worth pointing out that this property gives you the ability to convert one type to the other...

    type T struct{
        // ...
    }
    
    func (T) M() {}
    
    type U T
    
    func NewU() *U {
        return &U{}
    }
    
    func (U) N() {}
    
    func main() {
        u := NewU()
    
        u.M() // compile error
        u.N()
    
        // convert *U to *T and then call M
        (*T)(u).M()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题