【发布时间】:2021-04-02 22:06:44
【问题描述】:
您好,我实际上正在构建一个网站,该网站必须使用模板将文本转换为一些 ascii 艺术
如果我在等待退货,为什么会出现这个错误:
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
对于那些看过这篇文章的人,我很抱歉恐慌线被```删除了
这里是 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)
})
获取横幅函数是一个包含所有 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]
}
【问题讨论】:
-
您没有显示实际错误,只是显示错误发生位置的堆栈跟踪。 (错误应该就在你的输出之前,并以
panic:开头)。 -
请编辑您的问题以包含产生问题的相关代码。