I have a Form struct which holds a map of FormFields and a method to add
FormField structs.
The method to add FormField looks as follows:
func (f *Form) addField(fieldtype,fieldname string) {
switch fieldtype {
case "text":
ff := new(FormField_text)
case "hidden":
ff := new(FormField_hidden)
}
ff.name = fieldname
// ... more fields to be filled and things to be done identical for all
ff's
f.fields[fieldname] = ff
}
Problem: The compiler says that "fieldname" can not be assigned because
"ff" is not defined.
I can of course define "ff" outside the switch block using an interface:
func (f *Form) addField(fieldtype,fieldname string) {
var ff FormFieldInterface
switch fieldtype {
case "text":
ff = new(FormField_text)
case "hidden":
ff = new(FormField_hidden)
}
ff.name = fieldname
// ... more fields to be filled and things to be done identical for all
ff's
f.fields[fieldname] = ff
}
Problem: Now the compiler complains that the interface FormFieldInterface
has no field "name". That's correct, of course because I can not define the
fields for the interface but only for the structs that implement that
interface.
Obviously I do not want to repeat assignements and actions for the ff's
inside each switch case. But right now this seems to be the only working
solution.
Can anybody help me what I could do instead?
I have been programmed PHP and Javascript so far. Therefore, I am sorry if
this is a beginners question. All hints are highly appreciated.
Adrian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.