Why would a call to fmt.Sprint(e) infinite loop

newbieski·2023년 4월 10일
0

golang

목록 보기
9/13

https://stackoverflow.com/questions/27474907/why-would-a-call-to-fmt-sprinte-inside-the-error-method-result-in-an-infinit

fmt.Sprint(e)에서 e가 string이 아니고 errors면 무한루프를 돔. 왜?

fmt.Sprint(e) 코드가 대략 아래처럼 생격다고 함.
Stringer면 출력을 하는데 error면....

switch verb {
    case 'v', 's', 'x', 'X', 'q':
        // Is it an error or Stringer?
        // The duplication in the bodies is necessary:
        // setting handled and deferring catchPanic
        // must happen before calling the method.
        switch v := p.arg.(type) {
        case error:
            handled = true
            defer p.catchPanic(p.arg, verb, "Error")
            p.fmtString(v.Error(), verb)
            return

        case Stringer:
            handled = true
            defer p.catchPanic(p.arg, verb, "String")
            p.fmtString(v.String(), verb)
            return
        }
    }

어쨌든 String으로 변환해서 출력해야한다고 함

profile
newbieski

0개의 댓글