doutan1875
2016-02-02 04:22
浏览 374
已采纳

golang:http.get()返回

ok so yes im new to golang, but not to coding, I'm a bit confused with the way functions are working with golang, in this 2 weeks I have 9 times out or 10 I had an issue it was related to funtions... I'm no lazy I have search all over for examples to inspire me, but they are all under one main() funtion. I'm trying to have a http.get in one funtion and many other functions call this funtion everytime we need to use http.get so we dont repeat code over and over.. example:(this is not the actual code)

func myfunction(site) []byte {
        resp, err := client.Get(site) // client is because Im tunneling thing on a proxy TOR and had to create some helpers.. but this is working ok.

        return resp
}

func magic(staff) string {
        // do things and create websiteurl with staff and onther contants
        site := myfunction(website)
        contents, err := html.Parse(site.Body)
        //....
        //....
        return  result
}

main() {

        //... stuff happens :)
}

well the error is for example because I keep changing things around and getting different errors. or not at all.. but then no result..

cannot use resp.Body (type io.ReadCloser) as type []byte in return argument ./gobot.go:71: cannot use site (type []byte) as type io.Reader in argument to html.Parse:

when I do not get an error that is how I did it the first time the Site.Body will do nothing when parsing... I put a couple of debug prints to STDOUT and I had results like two sequence of numbers.

so basically how do I return a "result" of my query from one function to the original so it can then be parse and used? I hate to repeat code so trying to keep the repetitive code in one function and have it get it call when needed it.

thanks

图片转代码服务由CSDN问答提供 功能建议

好,所以是golang的新手,但不是编码,我对函数的方式有些困惑 在使用golang的过程中,这2周内我有9次或10次出现与功能有关的问题...我很懒,我到处都在寻找可以启发我的示例,但是它们全都在一个主要方面 ()函数。 我正在尝试将一个http.get包含在一个函数中,并且每当我们需要使用http.get时,许多其他函数都将其称为该函数,因此我们不会一遍又一遍地重复代码。.例如:(这不是 实际代码)

  func myfunction(site)[] byte {
 resp,err:= client.Get(site)//客户端是因为Im在代理上传输内容 TOR,必须创建一些帮助程序。..但是这可以正常工作。
 
返回resp 
} 
 
func magic(staff)字符串{
 //执行操作并创建带有职员和其他竞争者的网站网址
  site:= myfunction(网站)
内容,err:= html.Parse(site.Body)
 //....
 //。  ... 
返回结果
} 
 
main(){
 
 // ...发生了什么:)
} 
   
 
 

嗯,错误是因为例如我不断改变周围的东西并得到不同的错误。 还是根本没有..但是没有结果。

不能将 resp.Body (类型io.ReadCloser)用作 [] byte < / code>作为返回参数 。/gobot.go:71:无法将网站([] byte类型)用作io.Reader作为html.Parse的参数:

不会出现我第一次执行Site.Body 时所做的错误分析……我将几个调试打印输出到STDOUT,结果是两个数字序列。

基本上,如何将查询的“结果”从一个函数返回到原始函数,以便可以对其进行解析和使用? 我讨厌重复代码,因此尝试将重复代码保留在一个函数中,并在需要时调用它。

谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpo15099 2016-02-02 04:49
    已采纳

    myfunction has the wrong return type. Instead of []byte it should return *http.Response.

    func myfunction(site string) *http.Response {
            resp, err := client.Get(site) 
            if err != nil {
                    log.Fatal(err)
            }
    
            return resp
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题