|
| 1 | + |
| 2 | +# Clarifications |
| 3 | + |
| 4 | +- Don't think they should remove the feature in Go 1 |
| 5 | + - However, there is talk of removing it for Go 2 (which is a thing?) |
| 6 | + https://github.com/golang/go/issues/21291 |
| 7 | + |
| 8 | + SO IM DEFINITELY NOT CRAZY |
| 9 | + |
| 10 | + - I just wanted to make tons of Go puns |
| 11 | + - please rewatch the video if you didn't notice them. That means my delivery was perfect dad tier |
| 12 | + |
| 13 | + - I was just saying that the feature *should not have been made* not that it should be deleted. Those |
| 14 | + are two very different things when you're talking about an existing language, feature, product, etc. |
| 15 | + |
| 16 | +- Named Return vs. Naked Return |
| 17 | + - Named Return Values |
| 18 | + - So let's start with making it clear that I'm not opposed to this feature in general. |
| 19 | + - I think it's nice that you can "self-document" the code with naming the return values |
| 20 | + and that can show up in documentation |
| 21 | + |
| 22 | + - If naked returns did not exist, I would liked named returns better! |
| 23 | + |
| 24 | + - However, just because I like the feature doesn't mean that I'm confident in its worthiness of existing in Go. |
| 25 | + - is it really so much better than typing `var x, y int` at the top of your function? |
| 26 | + - They don't behave like other variables. Someone mentioned in the comments that they are initialized |
| 27 | + and returned, but I actually gave an example where they were never used and still didn't generate |
| 28 | + a warning or compiler error (which I think they should, to be consistent). |
| 29 | + - But in that case, it's probably super annoying to use and if you want to ignore some parameters, |
| 30 | + that doesn't work out super great. |
| 31 | + |
| 32 | + - Naked Return |
| 33 | + - I think I found out something even MORE horrible... defer functions |
| 34 | + |
| 35 | +```go |
| 36 | +func ReturnId() (id int, err error) { |
| 37 | + defer func() { |
| 38 | + if id == 10 { |
| 39 | + err = fmt.Errorf("Invalid Id\n") |
| 40 | + } |
| 41 | + }() |
| 42 | + |
| 43 | + id = 10 |
| 44 | + |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func ReturnId2() (id int, err error) { |
| 49 | + defer func(id int) { |
| 50 | + if id == 10 { |
| 51 | + err = fmt.Errorf("Invalid Id\n") |
| 52 | + } |
| 53 | + }(id) |
| 54 | + |
| 55 | + id = 10 |
| 56 | + |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +// RETURNS 2 ??? |
| 61 | +// As one would expect. The deferred code runs after the normal code in the |
| 62 | +// function has ended, and before the calling code has resumed. Named return |
| 63 | +// values are in scope and can be read and modified. |
| 64 | + func c() (i int) { |
| 65 | + defer func() { i++ }() |
| 66 | + return 1 |
| 67 | +} |
| 68 | + |
| 69 | +// RETURNS ERROR???? |
| 70 | +func test() (res int, err error) { |
| 71 | + res = 5 |
| 72 | + defer func() { err = fmt.Errorf("error in defer") }() |
| 73 | + return res, nil |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +// makes sense when you see it here but there's no way most people know this |
| 79 | + |
| 80 | +package main |
| 81 | + |
| 82 | +// Emonstrate that defered functions can chnage the return value |
| 83 | +// and one cannot simulate that with local variables. |
| 84 | + |
| 85 | +import ( |
| 86 | + "fmt" |
| 87 | +) |
| 88 | + |
| 89 | +func incrementer(i *int) { |
| 90 | + *i++ |
| 91 | +} |
| 92 | + |
| 93 | +func test1() (i int) { |
| 94 | + defer incrementer(&i) |
| 95 | + |
| 96 | + // Go copies 1 into the location named by "i" and then |
| 97 | + // calls defered functions. |
| 98 | + return 1 |
| 99 | +} |
| 100 | + |
| 101 | +func test2() int { |
| 102 | + i := 0 |
| 103 | + defer incrementer(&i) |
| 104 | + |
| 105 | + // The value named by "i" is copied first into the |
| 106 | + // return location and then the defered code is called. |
| 107 | + // Thus changes to i have no effect on the return value. |
| 108 | + return i |
| 109 | +} |
| 110 | + |
| 111 | +func main() { |
| 112 | + fmt.Println("named result:", test1()) |
| 113 | + fmt.Println("local variable:", test2()) |
| 114 | +} |
| 115 | +``` |
0 commit comments