Comments (3)
This seems to be expected given how defer
works in Go.
https://go.dev/play/p/eF8TmyEnTxt
func t1() int {
var i int
defer func() {
i++
}()
return i
}
func t2() (i int) {
defer func() {
i++
}()
return i
}
func main() {
fmt.Println(t1())
fmt.Println(t2())
}
Results in:
0
1
Program exited.
Even playing with pointers does not work around this:
func t3() int {
var i int
ii := &i
defer func() {
*ii++
}()
return *ii
}
// returns 0
My brief reading of the spec here https://go.dev/ref/spec#Defer_statements calls out that you need to be using named returns for defers to modify the return value:
For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned.
from multierr.
This is a good point, but yeah @rabbbit is right, because of how defers work, the library can't address this.
We can do two things here:
- Update the documentation of AppendInvoke to loudly state that if you're using it inside a defer, the variable must be a named return.
- In the future, we can look into building a go/analysis-based linter to catch these cases since this is pretty easily lintable.
from multierr.
Closing because documentation was updated in #63, and there's nothing we can do at the library level besides that.
from multierr.
Related Issues (17)
- Caller to 'demux' list of multiple errors returned HOT 3
- Support other multierr implementations HOT 2
- README says status "beta" HOT 1
- Wish we could tell if the append happened HOT 1
- Zap integration (again) HOT 6
- Go 1.13 unwrapping behavior HOT 2
- Depending on this project ends up pulling in lots of dependencies HOT 5
- go get uber-go / multierr error HOT 1
- LateAppendInto as convenient shorthand for io.Closer and similar use cases HOT 8
- Figure out integration story for logging errors, especially using zap HOT 7
- Invitation to comment on proposed standard library multierror support HOT 1
- Support multierr.Every HOT 3
- YAML package update HOT 2
- Go 1.20: Errors() should support any multi-error HOT 1
- Set minimum permissions to workflows HOT 1
- Equal Errors showing as different HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multierr.