FAQ
Hi

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.

Search Discussions

  • Nick Patavalis at Jan 12, 2014 at 6:31 pm
    The compiler is, obviously, right. When it sees

       ff.name = fieldname

    the compiler must know (or be able to deduce without ambiguity) the type of
    ff. This is not the case with your code.

    The only solution I can see (without repeating the assignment to "ff.name")
    is to add a "Name" method to the FormFieldInterface that sets the
    corresponding field (or fields) in the respective concrete types. if you
    think of it, this is the only conceptually-correct approach: Different
    structures, with different implementation, both providing the same method.
    The fact that you have two structures that HAPPEN to both have a field with
    the same name, does not make it the same field.

    /npat
    On Sunday, January 12, 2014 8:12:41 PM UTC+2, AKU wrote:

    Hi

    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.
  • AKU at Jan 12, 2014 at 6:55 pm
    Thank you for the quick answer.

    You are right. In fact, the struct fields ARE different depending on the
    FormField type.
    I will try using an init() method which I can implement specifically for
    each FormField type.

    It seems I still have too much PHP in my mind ;-).

    Thank you for helping me out.

    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.
  • Minux at Jan 12, 2014 at 7:00 pm

    On Jan 12, 2014 1:54 PM, "AKU" wrote:
    You are right. In fact, the struct fields ARE different depending on the
    FormField type.
    I will try using an init() method which I can implement specifically for
    each FormField type.
    another way is to extract these common fileds out into another "common"
    struct, implement the required Set method for it and then embed the common
    struct into every FormField struct so that you only need to write the Set
    method once.

    --
    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.
  • Matt Harden at Jan 13, 2014 at 1:49 am
    You could initialize the structs when you create them.

    func (f *Form) addField(fieldtype,fieldname string) {
       switch fieldtype {
         case "text":
           ff := &FormField_text{name: fieldname}
         case "hidden":
           ff := &FormField_hidden{name: fieldname}
       }
       ff.name = fieldname
       // ... more fields to be filled and things to be done identical for all
    ff's
       f.fields[fieldname] = ff
    }
    On Sunday, January 12, 2014, AKU wrote:

    Hi

    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 <javascript:_e({},
    'cvml', 'golang-nuts%2Bunsubscribe@googlegroups.com');>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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.
  • Adrian Kuehnis at Jan 13, 2014 at 5:06 am
    Thank you, this works perfectly.

    func (f *Form) addField(fieldtype,fieldname string) {
       var ff FormFieldInterface
       switch fieldtype {
         case "text":
           ff = &FormField_text{name:fieldname}
         case "hidden":
           ff = &FormField_hidden{name:fieldname}
       }
       // ... more ff methods to be called
       f.fields[fieldname] = ff
    }

    Thank you for your help!

    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 12, '14 at 6:12p
activeJan 13, '14 at 5:06a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase