Error checking
This commit is contained in:
		@ -1,6 +1,7 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -23,10 +24,29 @@ func addValues(x, y int) int {
 | 
				
			|||||||
	return x + y
 | 
						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
 | 
					// main is the main application function
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	http.HandleFunc("/", Home)
 | 
						http.HandleFunc("/", Home)
 | 
				
			||||||
	http.HandleFunc("/about", About)
 | 
						http.HandleFunc("/about", About)
 | 
				
			||||||
 | 
						http.HandleFunc("/divide", Divide)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Printf("Starting application on port %s\n", portNumber)
 | 
						fmt.Printf("Starting application on port %s\n", portNumber)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user