-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15243cf
commit 3963e02
Showing
1 changed file
with
30 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |