From c072558b90f1bbedc2022b0f30c8b1ac4712538e Mon Sep 17 00:00:00 2001
From: Ondřej Surý
-for i := 0; i < 5; i++ { +for i := 0; i < 5; i++ { defer fmt.Printf("%d ", i) }@@ -1486,7 +1486,7 @@ for a min function that chooses the least of a list of integers: func Min(a ...int) int { min := int(^uint(0) >> 1) // largest int for _, i := range a { - if i < min { + if i < min { min = i } } @@ -2670,7 +2670,7 @@ suppresses the usual check for a
return
statement.
// A toy implementation of cube root using Newton's method.
func CubeRoot(x float64) float64 {
z := x/3 // Arbitrary intitial value
- for i := 0; i < 1e6; i++ {
+ for i := 0; i < 1e6; i++ {
prevz := z
z -= (z*z*z-x) / (3*z*z)
if veryClose(z, prevz) {
@@ -2705,7 +2705,7 @@ func init() {
When panic
is called, including implicitly for run-time
-errors such indexing an array out of bounds or failing a type
+errors such as indexing an array out of bounds or failing a type
assertion, it immediately stops execution of the current function
and begins unwinding the stack of the goroutine, running any deferred
functions along the way. If that unwinding reaches the top of the
@@ -2727,7 +2727,7 @@ inside a server without killing the other executing goroutines.
-func server(workChan <-chan *Work) { +func server(workChan <-chan *Work) { for work := range workChan { go safelyDo(work) } @@ -2751,7 +2751,16 @@ callingrecover
handles the condition completely.-Note that with this recovery pattern in place, the
+ +do
+Becauserecover
always returnsnil
unless called directly +from a deferred function, deferred code can call library routines that themselves +usepanic
andrecover
without failing. As an example, +the deferred function insafelyDo
might call a logging function before +callingrecover
, and that logging code would run unaffected +by the panicking state. ++With our recovery pattern in place, the
do
function (and anything it calls) can get out of any bad situation cleanly by callingpanic
. We can use that idea to simplify error handling in complex software. Let's look at an @@ -2876,7 +2885,7 @@ func main() { } func QR(w http.ResponseWriter, req *http.Request) { - templ.Execute(req.FormValue("s"), w) + templ.Execute(w, req.FormValue("s")) } func UrlHtmlFormatter(w io.Writer, fmt string, v ...interface{}) { -- cgit v1.2.3