Go Anonymous Method For A Structure

huangapple go评论103阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2014年4月17日 21:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/23134439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定