1 year ago

#330211

test-img

Kota

How do I get mutiple files like html, css, javascript, and php in static

As I said the title, How do I get mutiple files like html, css, javascript, and php in static using main.go

package main

import (
    "fmt"
    "log"
    "net/http"
)

func formHandler(w http.ResponseWriter, r *http.Request) {
    if err := r.ParseForm(); err != nil {
        fmt.Fprintf(w, "ParseForm() err: %v", err)
        return
    }
    fmt.Fprintf(w, "POST request successful")
    name := r.FormValue("name")
    address := r.FormValue("address")
    fmt.Fprintf(w, "Name = %s\n", name)
    fmt.Fprintf(w, "Address = %s\n", address)
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path != "/hello" {
        http.Error(w, "404 not found", http.StatusNotFound)
        return
    }
    if r.Method != "GET" {
        http.Error(w, "method is not supported", http.StatusNotFound)
        return
    }
    fmt.Fprintf(w, "hello!")
}

func main() {
    fileServer := http.FileServer(http.Dir("./static/"))
    http.Handle("/static", fileServer)
    http.HandleFunc("/form", formHandler)
    http.HandleFunc("/hello", helloHandler)

    fmt.Printf("Starting server at port 8080\n")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}

also there are multiple files like this

enter image description here

but page shows wrong style, does anyone how to get these multiple files?

enter image description here

or how do i fix this? fileServer := http.FileServer(http.Dir("./static/"))

php

html

css

go

goland

0 Answers

Your Answer

Accepted video resources