Skip to content

Instantly share code, notes, and snippets.

@samuelastech
Created September 25, 2024 20:55
Show Gist options
  • Save samuelastech/a26d49b4aad559fc05869d74827c634a to your computer and use it in GitHub Desktop.
Save samuelastech/a26d49b4aad559fc05869d74827c634a to your computer and use it in GitHub Desktop.
Simple HTTP server with Go
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Server home")
})
http.HandleFunc("/resource", func(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Something bad occurred", http.StatusBadRequest)
return
}
log.Printf("Data was sent: %v\n", data)
})
log.Println("Server is running at 8000")
http.ListenAndServe(":8000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment