Setting Up a Fast and Simple HTTP Web Server in Go (Golang)
- Category Scripts
- Type Script
- Platform Cross-platform
- Language Go
- Price Free
- Copy 7 898
- Comments 0
Setting Up a Fast and Simple HTTP Web Server in Go (Golang)
package main
import (
"fmt"
"net/http"
)
// Handler function to process incoming user web requests
func helloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"message": "Welcome to your Go Snippet API!"}`)
}
func main() {
// Define the endpoint route and its corresponding handler
http.HandleFunc("/api/welcome", helloHandler)
fmt.Println("Server successfully started on local port :8080...")
// Start listening on port 8080 and log errors if they happen
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.


There are no comments yet :(