如何解决如果我有回报,为什么我有一个 goroutine
您好,我实际上正在构建一个网站,该网站必须使用模板将文本转换为一些 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,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,0xc0001a2100)
C:/Go/src/net/http/server.go:2042 +0x4b
net/http.(*ServeMux).ServeHTTP(0x840920,0xc0001a2100)
C:/Go/src/net/http/server.go:2417 +0x1b7
net/http.serverHandler.ServeHTTP(0xc000126000,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)
})
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 服务器可能一次只能为一个客户端提供服务。
如果您显示错误是什么,我们可以提供有关如何对其进行故障排除的更多信息。
,int main()
{
john.id = 100;
john.grade = 80;
FILE* f = fopen("test","w+"); // check return value..
struct Student steve;
fwrite(&john,sizeof(struct Student),1,f); // same here...
fseek(f,SEEK_SET); // <-- reset offset,(or lseek if you're using a file descritor)
fread(&steve,f); // and here
printf("\n%d %d \n",steve.id,steve.grade);
fclose(f);
}
可能会多次自行执行,因此这里有一种绕过问题的方法:
http.HandleFunc
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。