Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are Go methods first class functions?

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*/
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading