正常在写一些爬虫代码的时候多少都会出点小问题,一位合格的程序员应该会认真排除并加以解决错误,如果是新手看着满屏的代码可能会一筹莫展。
golang爬某网站代码优化后,运行报了如下的错,找了半小时才找到原因,在此记录一下。
![](https://ask.qcloudimg.com/http-save/yehe-2511396/rc6mum80b8.jpeg?imageView2/2/w/2560/h/7000)
代码是这样的:
有一个interface类型的Parser:
type Parser interface {
Parser(contents []byte, url string) ParserResult
Serialize() (funcName string, args interface{})
}
有一个struct类型的FuncParser:
type FuncParser struct {
parser ParserFunc
funcName string
}
FuncParser 实现了Parser 接口:
func (f *FuncParser) Parser(contents []byte, url string) ParserResult {
return f.Parser(contents, url)
}
func (f *FuncParser) Serialize() (funcName string, args interface{}) {
return f.funcName, nil
}
抛开爬虫代码整体的复杂度,将代码简化到如下这样:
type ParserFunc func(url string) string
type FuncParser struct {
parser ParserFunc
}
func (f *FuncParser) Parser(url string) string {
return f.Parser(url)
}
func main() {
funcParse := FuncParser{
func(url string) string {
return url
},
}
funcParse.Parser("http://www.zhenai.com/zhenghun")
}
运行代码后同样会报错:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x467297, 0xe)
D:/Program Files/Go/go103/src/runtime/panic.go:616 +0x88
runtime.newstack()
D:/Program Files/Go/go103/src/runtime/stack.go:1054 +0x72d
runtime.morestack()
D:/Program Files/Go/go103/src/runtime/asm_amd64.s:480 +0x91
这个示例就很明显了,FuncParser的Parser方法里形成了递归调用(自己调自己),递归调用自身导致栈溢出,导致报错。应该改成这样:(小写的parser)
![](https://ask.qcloudimg.com/http-save/yehe-2511396/jds7v6fofu.png?imageView2/2/w/2560/h/7000)
实际上goland开发工具里已经提示了Recursive Call
![](https://ask.qcloudimg.com/http-save/yehe-2511396/ldn7lcmzam.png?imageView2/2/w/2560/h/7000)
一不小心就会写出这种代码,再看如下代码:
package main
import (
"fmt"
)
type Str string
func (s Str) String() string {
return fmt.Sprintf("Str: %s", s)
}
func main() {
var s Str = "hi"
fmt.Println(s)
}
同样报错:
![](https://ask.qcloudimg.com/http-save/yehe-2511396/g92pzurw9j.jpeg?imageView2/2/w/2560/h/7000)
You are implementing Str.String in terms of itself. return fmt.Sprintf("Str: %s", s) will call s.String(), resulting in infinite recursion. Convert s to string first.
This is working as intended, you are using the %s verb to call Str's String method, which uses fmt.Sprint to call Str's String method, and so on.
正常代码应该如下:
![](https://ask.qcloudimg.com/http-save/yehe-2511396/eel4k4nu0c.png?imageView2/2/w/2560/h/7000)
实际上,goland开发工具里也会警告该问题的:
![](https://ask.qcloudimg.com/http-save/yehe-2511396/bf0n5o2br9.png?imageView2/2/w/2560/h/7000)
看来平时编写代码,警告还是得注意的。
标签:FuncParser,func,string,遇到,Parser,爬虫,问题,Str,runtime From: https://www.cnblogs.com/q-q56731526/p/17285462.html