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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败