Skip to content

Instantly share code, notes, and snippets.

@hudson-newey
Last active November 13, 2023 11:59
Show Gist options
  • Save hudson-newey/787c4db7a05bbc769a2544d0bc99f7cf to your computer and use it in GitHub Desktop.
Save hudson-newey/787c4db7a05bbc769a2544d0bc99f7cf to your computer and use it in GitHub Desktop.
A simple golang server for serving a single file
package main
import (
"io"
"log"
"net/http"
"io/ioutil"
"os"
)
func main() {
// Set routing rules
http.HandleFunc("/", home)
//Use the default DefaultServeMux.
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func home(w http.ResponseWriter, r *http.Request) {
programName := os.Args[1]
io.WriteString(w, readFile(programName))
}
func readFile(fileName string) string {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return "Error 404, File not Found!"
}
return string(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment