You can't define methods inside a function, for details see Golang nested class inside function.
What you may do is create an implementation of SObject
which is capable of dispatching to custom functions in its own implementation. An easy way to create such "proxy" implementation is to use a struct with fields of function types matching the types of the methods (without a receiver), and implement methods on this struct. The method implementations may simply forward the calls to the function values stored in the struct fields, and may have a default behavior if the appropriate field is not set. And you may change the values of the function fields, the behavior of the methods will be dynamic.
Here's an example of it:
type sobjimpl struct {
apiName func() string
externalIdApiName func() string
}
func (s *sobjimpl) ApiName() string {
if s.apiName == nil {
return "<not implemented>"
}
return s.apiName()
}
func (s *sobjimpl) ExternalIdApiName() string {
if s.externalIdApiName == nil {
return "<not implemented>"
}
return s.externalIdApiName()
}
func createApiNameMethod(name string) SObject {
return &sobjimpl{
apiName: func() string { return name },
}
}
Testing it:
so := createApiNameMethod("testName")
fmt.Println(so.ApiName())
Output is as expected (try it on the Go Playground):
testName
Of course in this simple example the sobjimpl.apiName
function field could be left out entirely, it could have been enough to just store the name
and return that from sobjimpl.ApiName()
. But the example shows how you may choose functions at runtime that will be called as the methods of an implementation.