Home > front end >  Are Go methods first class functions?
Are Go methods first class functions?

Time:01-14

The title basically says it all..

Can I create a Go method that returns another Go method, at runtime? A simple example:

type Person struct {
    name string
    age uint
}

func (p Person) createGetNameMethod() /*return signature is a method for Person*/ {
    return /*return a new anonymous method here for Person*/
}

CodePudding user response:

Are Go methods first class functions?

Yes, they are.

Can I create a Golang method that returns another Golang method [...]?

Yes, of course.

[Can I] return a new anonymous method [?]

No, of course not.

The set of methods is determined at compile time. Methods are normal, first class functions, but they cannot be changed or created during runtime:

  • You can return a method that exists in the method set, but you cannot add one to the method set.

  • Reflection allows something like that but not in your case.

  •  Tags:  
  • Related