如果我有 return 为什么还要有 goroutine

why do I have a goroutine if I have a return

你好,我实际上正在构建一个网站,该网站必须使用模板将文本转换为某种 ascii 艺术

如果我在等待 return 为什么会出现此错误:

2021/04/02 23:50:57 http: panic serving [::1]:64796: runtime error: index out of range [0] with length 0
goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc00009d040)
        C:/Go/src/net/http/server.go:1801 +0x147
panic(0x5c8040, 0xc0001ae4e0)
        C:/Go/src/runtime/panic.go:975 +0x3e9
Ascii-art-web/AsciiArts.AsciiMain(0x0, 0x0, 0x0, 0x0, 0xc0001802c0, 0x659920, 0x2, 0x5e7c7d)
        C:/Users/kioki/go/src/Ascii-art-web/AsciiArts/asciimain.go:38 +0x766
main.main.func1(0x65e4c0, 0xc0001a82a0, 0xc0001a2100)
        C:/Users/kioki/go/src/Ascii-art-web/server.go:42 +0x1ca
net/http.HandlerFunc.ServeHTTP(0xc000088d30, 0x65e4c0, 0xc0001a82a0, 0xc0001a2100)
        C:/Go/src/net/http/server.go:2042 +0x4b
net/http.(*ServeMux).ServeHTTP(0x840920, 0x65e4c0, 0xc0001a82a0, 0xc0001a2100)
        C:/Go/src/net/http/server.go:2417 +0x1b7
net/http.serverHandler.ServeHTTP(0xc000126000, 0x65e4c0, 0xc0001a82a0, 0xc0001a2100)
        C:/Go/src/net/http/server.go:2843 +0xaa
net/http.(*conn).serve(0xc00009d040, 0x65eb40, 0xc000184000)
        C:/Go/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
        C:/Go/src/net/http/server.go:2969 +0x36d

对于那些看过这个的人post很抱歉恐慌线被```删除了

这里是 asciimain.go 文件 l38 = s := GetBanner(chars[0])

func AsciiMain(font string,str string) []string {
    chars := []byte(str)
    s := GetBanner(chars[0])
    for i := 1;i < len(chars); i++ {
        g := GetBanner(chars[i])
        for j := 0;j < 8; j++ {
            s[j] = s[j]+g[j]
        }
    }
    return s
}

str 是由用户在网站上发送的 t 和 server.go 文件:

http.HandleFunc("/",func( w http.ResponseWriter, r *http.Request){
    details := AsciiSubmit{
        Color:   r.FormValue("colorpicker"),
        Font: r.FormValue("font"),
        Text: r.FormValue("message"),
    }
    fstr := AsciiArts.AsciiMain(details.Font,details.Text)

    data := Page{"AsciiArts",details.Color,fstr[0],fstr[1],fstr[2],fstr[3],fstr[4],fstr[5],fstr[6],fstr[7]}

    tmpl.ExecuteTemplate(w, "index", data)
})

get banner func 是一张包含所有 ascii 艺术的地图:

func GetBanner(stri byte) []string {
    font := make(map[byte][]string, 100)
    font[32] = []string{
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
    }
    font[33] = []string{
        " _  ",
        "| | ",
        "| | ",
        "| | ",
        "|_| ",
        "(_) ",
        "    ",
        "    ",
    }
    ...
    font[126] = []string{
        " /\/| ",
        "|/\/ ",
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
        "      ",
    }
    if v, ok := font[stri]; ok {
        return v
    }
    return font[32]
}

这是运行时恐慌。为什么它恐慌也应该在这个堆栈转储之前的某个地方打印出来。

HTTP 服务器在自己的 goroutine 中处理每个请求。如果不是这种情况,http 服务器一次只能为一个客户端提供服务。

如果您说明错误是什么,我们可以提供有关如何解决问题的更多信息。

http.HandleFunc 可能会自行执行多次,所以这里有一个绕过问题的方法:

http.HandleFunc("/",func( w http.ResponseWriter, r *http.Request){
        fmt.Println("ici")
        tmpl, _ := textTemplate.ParseFiles("./html/index.html")
        details := AsciiSubmit{
            Color:   r.FormValue("colorpicker"),
            Font: r.FormValue("font"),
            Text: r.FormValue("message"),
        }
        if len(details.Text) != 0 {
            fstr := AsciiArts.AsciiMain(details.Font,details.Text)
            data := Page{"AsciiArts",details.Color,fstr[0],fstr[1],fstr[2],fstr[3],fstr[4],fstr[5],fstr[6],fstr[7]}

            tmpl.Execute(w, data)
        } else {
            data := Page{Title: "AsciiArts"}

            tmpl.Execute(w, data)
        }


    })