# Typo in exemple code of the Go Blog?

**URL:** <https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329>\
**Category:** Site Feedback\
**Created:** [February 28, 2018, 3:19pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329 "2018-02-28T15:19:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![saintlouis](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/saintlouis/32/3810_2.png) [@saintlouis](https://forum.golangbridge.org/u/saintlouis)\
**Post date:** [February 28, 2018, 3:19pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/1 "2018-02-28T15:19:28Z")

</div>

Here the link to the page in question: [https://blog.golang.org/defer-panic-and-recover](https://blog.golang.org/defer-panic-and-recover)

The exemple code as is in the page is

```auto
func c() (i int) {
    defer func() { i++ }()
    return 1
}

```

And I think it should `return i`instead. Am I wrong?  
So it should be:

```auto
func c() (i int) {
    defer func() { i++ }()
    return i
}

```

---

<div class="post-metadata">

**Author:** ![Alberto\_Bregliano](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/alberto_bregliano/32/3802_2.png) [@Alberto\_Bregliano](https://forum.golangbridge.org/u/Alberto_Bregliano)\
**Post date:** [February 28, 2018, 3:40pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/2 "2018-02-28T15:40:03Z")

</div>

> [@saintlouis](#):
>
> defer func() { i++ }()  
> return i  
> }

Yeah I believe you are right

---

<div class="post-metadata">

**Author:** ![nathankerr](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nathankerr/32/2287_2.png) [@nathankerr](https://forum.golangbridge.org/u/nathankerr)\
**Post date:** [February 28, 2018, 6:46pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/3 "2018-02-28T18:46:48Z")

</div>

The text above that code says:

> In this example, a deferred function increments the return value i after the surrounding function returns. Thus, this function returns 2:

At the beginning of `c(0)`, `i` is 0 (the zero value for an `int`). For the function to return 2, `i` needs to be 1 so the increment done in the `defer`red function increases the returned value (`i`) to 2. `return 1` sets `i` to 1.

Since a `return` statement is required for functions that return values, this single line `return 1` is the easiest way to both set `i` to 1 (as required by the text) and `return`.

---

<div class="post-metadata">

**Author:** ![saintlouis](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/saintlouis/32/3810_2.png) [@saintlouis](https://forum.golangbridge.org/u/saintlouis)\
**Post date:** [February 28, 2018, 10:54pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/4 "2018-02-28T22:54:49Z")

</div>

Thank you very much nathankerr. Your reply made me realize that in `func c() (i int) {` the `(i int)` part is actually declaring the _returning_ variable. It is not, actually, the parameter part in the declaration, which is empty.

I completely overlooked that. And frankly I find it quite misleading/confusing. It is something to look after in production code I guess. My eyes are so used to C code (or whatever languages), it did not see that caveat.

Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![nathankerr](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nathankerr/32/2287_2.png) [@nathankerr](https://forum.golangbridge.org/u/nathankerr)\
**Post date:** [February 28, 2018, 11:09pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/5 "2018-02-28T23:09:20Z")

</div>

I try to use named return values only when they are needed, such as when [handling `error`s in `defer`](https://pocketgophers.com/handling-errors-in-defer/) or recovering from `panic` and a return value needs to be set.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 29, 2018, 11:09pm UTC](https://forum.golangbridge.org/t/typo-in-exemple-code-of-the-go-blog/8329/6 "2018-05-29T23:09:34Z")

</div>

Closed on May 29, 2018, 11:09pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
