There's something wrong with the reptile

Keywords: Go github Java Python Big Data

After the code optimization of golang climbing treasure net, the following error was reported. It took half an hour to find out the reason. Record here.

The code is as follows:

There is a Parser of type interface:

type Parser interface {
    Parser(contents []byte, url string) ParserResult
    Serialize() (funcName string, args interface{})
}

There is a FuncParser of type struct:

type FuncParser struct {
    parser ParserFunc
    funcName string
}

FuncParser implements the Parser interface:

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
}

Regardless of the overall complexity of the crawler code, the code is simplified as follows:

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")
}

The same error will be reported after running the code:

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

In this example, it is obvious that recursive calls (call yourself) are formed in the Parser of FuncParser.
Recursive call itself causes stack overflow and error reporting. It should be changed to this: (lower case parser)

Actually, in goland, there's a reminder call.

If you are not careful, you will write this code. Look at the following code:

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)
}

The same error is reported:

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.

The normal code should be as follows:

In fact, goland will also warn about the problem:

It seems that when you write code, you should pay attention to warnings.

Project code is shown in: https://github.com/ll837448792/crawler

This public account provides free csdn download service and massive it learning resources. If you are ready to enter the IT pit and aspire to become an excellent program ape, these resources are suitable for you, including but not limited to java, go, python, springcloud, elk, embedded, big data, interview materials, front-end and other resources. At the same time, we have set up a technology exchange group. There are many big guys who will share technology articles from time to time. If you want to learn and improve together, you can reply [2] in the background of the public account. Free invitation plus technology exchange groups will learn from each other and share programming it related resources from time to time.

Scan the code to pay attention to the wonderful content and push it to you at the first time

Posted by robin105 on Thu, 17 Oct 2019 15:14:25 -0700