55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
// About is the about page handler
|
|
func Divide(w http.ResponseWriter, r *http.Request) {
|
|
f, err := divideValues(100.0, 0.0)
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Cannot divide by 0\n")
|
|
return
|
|
}
|
|
fmt.Fprintf(w, "%f divided by %f is %f\n", 100.0, 0.0, f)
|
|
}
|
|
|
|
func divideValues(x, y float32) (float32, error) {
|
|
if y <= 0 {
|
|
err := errors.New("cannot divide by zero")
|
|
return 0, err
|
|
}
|
|
return x / y, nil
|
|
}
|
|
|
|
// main is the main application function
|
|
func main() {
|
|
http.HandleFunc("/", Home)
|
|
http.HandleFunc("/about", About)
|
|
http.HandleFunc("/divide", Divide)
|
|
|
|
fmt.Printf("Starting application on port %s\n", portNumber)
|
|
|
|
_ = http.ListenAndServe(portNumber, nil)
|
|
}
|