udemy-go-web-1/cmd/hello-world/main.go

35 lines
704 B
Go
Raw Normal View History

2024-06-26 15:30:39 +00:00
package main
import (
"fmt"
"net/http"
)
2024-06-26 15:41:50 +00:00
const portNumber = ":8080"
// Home is the about page handler
func Home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is the home page")
}
// About is the about page handler
func About(w http.ResponseWriter, r *http.Request) {
sum := addValues(2, 2)
fmt.Fprintf(w, "This is the about page and 2 + 2 is %d", sum)
}
// addValues adds two integers and returns the sum
func addValues(x, y int) int {
return x + y
}
// main is the main application function
2024-06-26 15:30:39 +00:00
func main() {
2024-06-26 15:41:50 +00:00
http.HandleFunc("/", Home)
http.HandleFunc("/about", About)
fmt.Printf("Starting application on port %s\n", portNumber)
_ = http.ListenAndServe(portNumber, nil)
2024-06-26 15:30:39 +00:00
}