dongyunshan4066 2015-08-17 18:21
浏览 19
已采纳

当它说它属于另一种类型时,我怎么能使用packages方法呢?

Sorry for the horrible horrible title, first off if anybody can offer an edit for a better title after reading my question please, submit it, I'm pretty bad with my terminology at the moment.

So, simple question:

Reading through the net/http package on how to make http.Get requests and it says all I have to do is

resp, err := http.Get(blah)

Ok fair enough so scrolling down the list to see what parameters this Get function took, I couldn't find it directly under the functions of the http package

So scrolling down I find a Get method under type Client

So how come I don't have to first http.Client then make my Get request to that? Just a little confused. Thanks for any help.

  • 写回答

1条回答 默认 最新

  • doulu1907 2015-08-17 18:24
    关注

    Those are two different versions of the method. In one case; http.Get it's defined at the package level, this works much like a static method in C# or Java. In the other it has a receiver of type http.Client it's more like an instance method on that type in C# or Java. The type http.Client is as you'd expect in the same package.

    package level get: http://golang.org/pkg/net/http/#Get

     func Get(url string) (resp *Response, err error)
        //^ absence of receiver = package scoped
        //^ uppercase method name so it is 'exported' which is about like public
    

    client receiver get: http://golang.org/pkg/net/http/#Client.Get

       func (c *Client) Get(url string) (resp *Response, err error)
            //^ this is the receiver
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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