admin管理员组文章数量:1386846
I'm used to programming in javascript where I can do the following to pass an argument into an immediately-invoked function expression:
(function(twoSeconds) {
// do something with "twoSeconds" here
})(2 * 1000);
So I expected to be able to do something similar in Go, as below. However, it doesn't seem to work.
func (twoSeconds) {
// build error: "twoSeconds" undefined
}(time.Second * 2)
So I have to do this instead:
func () {
twoSeconds := time.Second * 2
}()
Therefore, my question is how can I pass an argument into a Go IIFE? And if it's not possible, why not?
I'm used to programming in javascript where I can do the following to pass an argument into an immediately-invoked function expression:
(function(twoSeconds) {
// do something with "twoSeconds" here
})(2 * 1000);
So I expected to be able to do something similar in Go, as below. However, it doesn't seem to work.
func (twoSeconds) {
// build error: "twoSeconds" undefined
}(time.Second * 2)
So I have to do this instead:
func () {
twoSeconds := time.Second * 2
}()
Therefore, my question is how can I pass an argument into a Go IIFE? And if it's not possible, why not?
Share Improve this question edited Oct 17, 2016 at 14:27 Mr_Pink 110k17 gold badges283 silver badges272 bronze badges asked Oct 17, 2016 at 8:47 user162097user162097 1,25811 silver badges21 bronze badges1 Answer
Reset to default 11Function arguments in Go need types. So do the following:
func(twoSeconds time.Duration) {
// use twoSeconds
}(time.Second * 2)
本文标签: Passing argument to Go IIFE (following javascript example)Stack Overflow
版权声明:本文标题:Passing argument to Go IIFE (following javascript example) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744572868a2613444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论