-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
34 lines (28 loc) · 1.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Package declaration indicating that this Go file is part of the 'main' package.
package main
// Importing necessary packages from the standard library.
import (
"fmt" // Package for formatted I/O.
"html" // Package for HTML escaping.
"log" // Package for logging.
"net/http" // Package for HTTP server functionality.
)
// The main function, which is the entry point of the program.
func main() {
// Registering a handler function for the root path ("/").
http.HandleFunc("/", rootHandler)
// Registering a handler function for the "/hello" path.
http.HandleFunc("/hello", helloHandler)
// Starting the HTTP server on port 8080 and handling requests using the default ServeMux.
log.Fatal(http.ListenAndServe(":8080", nil))
}
// Handler function for the root path ("/").
func rootHandler(w http.ResponseWriter, r *http.Request) {
// Responding with a formatted string containing the escaped URL path.
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
// Handler function for the "/hello" path.
func helloHandler(w http.ResponseWriter, r *http.Request) {
// Responding with a simple "Hello" message.
fmt.Fprintf(w, "Hello")
}