Skip to content

Instantly share code, notes, and snippets.

@harryhanYuhao
Last active February 25, 2024 13:36
Show Gist options
  • Save harryhanYuhao/9d9ec42ab97a526c799ddff965f636a0 to your computer and use it in GitHub Desktop.
Save harryhanYuhao/9d9ec42ab97a526c799ddff965f636a0 to your computer and use it in GitHub Desktop.
Go minimal server
// to run: go mod init <modname>; go tidy; go run .
package main
import (
"fmt"
"log"
"net/http"
"github.com/davecgh/go-spew/spew"
)
func syncData(w http.ResponseWriter, r *http.Request) {
// get id passed as `...?id=2111`
// id := r.URL.Query().Get("id")
// spew is for pretty printing
fmt.Fprintf(w, spew.Sdump(r))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", syncData)
// file server
//mux.Handle("/download/", http.StripPrefix("/download/", http.FileServer(http.Dir(fileDir))))
log.Fatal(http.ListenAndServe(":8080", mux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment