英文:
golang: http.get() returns
问题
好的,以下是翻译好的内容:
好的,是的,我对Go语言是新手,但对编程并不陌生。我对Go语言中函数的工作方式有些困惑,在过去的两周里,我遇到的问题中有9次中的10次都与函数有关...我并不懒惰,我已经在各处搜索了示例来给我灵感,但它们都在一个main()函数下面。
我想在一个函数中使用http.get,并且让许多其他函数在需要使用http.get时每次调用这个函数,这样我们就不会重复编写代码。示例代码如下(这不是实际的代码):
func myfunction(site) []byte {
resp, err := client.Get(site) // client是因为我在代理TOR上进行了一些调整,所以必须创建一些辅助函数...但这个工作正常。
return resp
}
func magic(staff) string {
// 做一些事情并使用staff和其他常量创建websiteurl
site := myfunction(website)
contents, err := html.Parse(site.Body)
//....
//....
return result
}
func main() {
//... 发生一些事情 :)
}
错误的例子是因为我一直在改变代码并得到不同的错误,或者根本没有结果...
无法将resp.Body
(类型为io.ReadCloser)作为返回参数中的[]byte
类型使用
./gobot.go:71: 无法将site(类型为[]byte)作为html.Parse的参数中的io.Reader类型使用
当我没有得到错误时,这就是我第一次编写的代码,Site.Body在解析时不起作用...我在标准输出中放了一些调试打印,结果是两个数字序列。
所以基本上,我如何将查询的"result"从一个函数返回给原始函数,以便可以解析和使用它?
我不想重复编写代码,所以尝试将重复的代码放在一个函数中,并在需要时调用它。
谢谢。
英文:
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
答案1
得分: 2
myfunction
的返回类型错误。它应该返回 *http.Response
而不是 []byte
。
func myfunction(site string) *http.Response {
resp, err := client.Get(site)
if err != nil {
log.Fatal(err)
}
return resp
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论