Skip to content

Commit

Permalink
added code for lecture 26
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Feb 27, 2023
1 parent 15243cf commit 3963e02
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
package main

import (
"errors"
"fmt"
"net/http"
)

const portNumber = ":8080"

// Home is the handler for the home page
func Home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is the home page")
}

// About is the handler for the about page
func About(w http.ResponseWriter, r *http.Request) {
owner, saying := getData()
sum := addValues(2, 2)
fmt.Fprintf(w, fmt.Sprintf("This is the about page of %s, \nI like to say %s, \nand as a side note, 2 + 2 is %d", owner, saying, sum))
_, _ = fmt.Fprintf(w, fmt.Sprintf("This is the about page and 2 + 2 is %d", sum))
}

// Divide divides one value into another and returns message with result
func Divide(w http.ResponseWriter, r *http.Request) {
x := 100.0
y := 0.0
f, err := divideValues(x, y)
if err != nil {
_, _ = fmt.Fprintf(w, "Error: Division by zero is not a valid operation. Error returned: %s", err)
return
}

_, _ = fmt.Fprintf(w, fmt.Sprintf("%f divided by %f is %f", x, y, f))
}

// divideValues divides two floats x and y, and returns the quotient and a value of type error
func divideValues(x, y float64) (float64, error) {
if y == 0 {
err := errors.New("Divisor is zero!")
return 0, err
}
result := x / y
return result, nil
}

// addValues adds two ints x and y, and returns the sum
func addValues(x, y int) int {
return x + y
}

// getData returns a name and a saying
func getData() (string, string) {
o := "Rick Sanchez"
s := "Wubba Lubba Dup Dup!"
return o, s
}

// main is the main function
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/about", About)
http.HandleFunc("/divide", Divide)

_ = http.ListenAndServe(":8080", nil)
fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
_ = http.ListenAndServe(portNumber, nil)
}

0 comments on commit 3963e02

Please sign in to comment.