Search code examples
htmlgogoroutine

why do I have a goroutine if I have a return


Hello I am actually building a website which has to transform a text to some ascii art with a template

So if I am waiting for a return why do I have this error:

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

for those who have seen this post I am sorry the panic line got erased by the ```

Here is the asciimain.go file 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
}

the str is sent by the user on the website here it was t and the server.go file:

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

The get banner func is a map with all the ascii art in it:

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

Solution

  • The http.HandleFunc might execute it self multiple times so here is a way to bypass the problem:

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