I need to do something like this:
// I have an interface with these two methods defined
type mapper interface {
ReadMapper(interface{}) interface{}
WriteMapper(interface{}) interface{}
}
then I need to assign ReadMapper
and WriteMapper
some functions which are defined somewhere else in the project. Is there is a way to do something like this in Go? Assigning functions to them?
V1Mapper mapper
V1Mapper.ReadMapper = functions()....
V1Mapper.WriteMapper = functions()....
Then, I need another map and want the mapper as the value type and want to do something like below.
mappingFunctions := map[string]mapper
mappingFunctions ["some_string"] = V1Mapper
and then from another file, I want to do this:
mappingFunctions["some_string"].ReadMapper(Data)
EDIT: I never asked for syntax in the question. I just wanted to know if there was a better way to do this because I was new to Go. I do agree I am not very much familiar with the syntax of the Go and I will have to go through the documentation (not once but many times).
If you want to compose mappers from the existing functions, you can use structures instead of interfaces:
type Mapper struct {
ReadMapper func(interface{}) interface{}
WriteMapper func(interface{}) interface{}
}
mappers := map[string]*Mapper{}
mappers["some_name"] = &Mapper{
ReadMapper: someFunc1,
WriteMapper: someFunc2,
}
or
m := new(Mapper)
m.ReadMapper = someFunc1
m.WriteMapper = someFunc2
mappers["some_name"] = m