英文:
Go Anonymous Method For A Structure
问题
Go语言支持结构体的匿名方法吗?如果支持,如何创建和调用它们?
这是我一直在尝试让它工作的代码,但我不确定Go(go version go1.1.2 linux/amd64)是否支持结构体的匿名方法。
package main
import (
"fmt"
)
type Person struct{
name string
age int
}
func (p Person) get_details() string {
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
func main() {
p := Person{name:"G4143", age:5}
// 简单的匿名函数,可以正常工作
fmt.Println(func(i int)int{return i * i}(34))
str := p.get_details()
fmt.Println(str)
// 无法编译的匿名方法
str = p.func(p Person)()string{return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age) }()
fmt.Println(str)
}
感谢您的指导。
英文:
Does Go support anonymous methods for structures and if they do how do you create and call them?
This is the code I've been trying to get work but I'm unsure if Go(go version go1.1.2 linux/amd64) supports anonymous methods to structures.
package main
import (
"fmt"
)
type Person struct{
name string
age int
}
func (p Person) get_details() string {
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
func main() {
p := Person{name:"G4143", age:5}
//simple anonymous function which works
fmt.Println(func(i int)int{return i * i}(34))
str := p.get_details()
fmt.Println(str)
//anonymous method which won't compile
str = p.func(p Person)()string{return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age) }()
fmt.Println(str)
}
I thank-you for any guidance..
答案1
得分: 3
不,这是不可能的,但更重要的是,你不需要那样做,因为闭包系统已经让p
在你的函数中可用,就像外部作用域的其他变量一样。
str := func() string {
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}()
英文:
No, that's not possible, but more importantly you don't need that because of the closure system : p
is already available in your function, like the other variables of the external scope.
str = func()string{return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age) }()
答案2
得分: 3
你不能这样做,但是你有3个选项:
私有方法:
type Person struct{
name string
age int
}
func (p Person) Details() string { // 公有方法
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
func (p Person) details() string { // 私有方法,注意小写的D
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
使用内联函数,就像这样:
str = func() string {
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}()
将变量传递给内联函数:
str = func(p Person) string {
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}(p)
英文:
You can't do that, however you have 3 options :
Private method :
type Person struct{
name string
age int
}
func (p Person) Details() string { // public
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
func (p Person) details() string { // private, notice the lowercase D
return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age)
}
Use the inline function just like this :
str = func()string{return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age) }()
Pass the variable to the inline function :
str = func(p Person) string { return fmt.Sprintf("Name->%s, Age->%d", p.name, p.age) }(p)
答案3
得分: -2
不行。由于这个答案太简短:真的没有办法做到这一点。顺便说一句,我看不出有任何理由这样做。
英文:
No. As this answer is to short: There is really no way to do this. BTW I cannot see any reason to do this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论