FAQ
Hello everyone, in the following code how do I avoid >> d.ThisType "is not
a type". I cant find the answer anywhere...

...please let me know what to do to change my variable so it works in the
type assertion, I presumed type assertion takes reflect.Value


type DataType struct {
ID string
ThisType reflect.Value
channel chan interface{}
}

func data_channel(d *DataType) {
for {
object := (<-d.channel).(d.ThisType)
switch(d.ID) {
case "wake_up":
// insert operational code here
break
case "shut_down":
// insert operational code here
break
case "new_instruction":
// insert operational code here
break
}
}
}

--
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/d/optout.

Search Discussions

  • Chris dollin at Aug 23, 2014 at 6:27 pm

    On 23 August 2014 17:41, Alex Howard wrote:
    Hello everyone, in the following code how do I avoid >> d.ThisType "is not a
    type". I cant find the answer anywhere...

    ...please let me know what to do to change my variable so it works in the
    type assertion, I presumed type assertion takes reflect.Value
    It doesn't. In a type assertion `something.(aType)`, aType is a
    type. Not a reflect.Type, a type, like `string` or `[]int` or a type name.

    Hence the message

         d.ThisType is not a type

    because it isn't.

    Chris

    --
    Chris "allusive" Dollin

    --
    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/d/optout.
  • Rob Pike at Aug 23, 2014 at 6:28 pm
    Type assertions take interface values, not reflect.Values, so your
    code is very wrong. Since your code doesn't even use the variable
    named object, it's impossible to say what you should do instead.

    If you're new to Go and you think you need reflection, you're probably
    doing something wrong.

    -rob

    On Sat, Aug 23, 2014 at 9:41 AM, Alex Howard wrote:
    Hello everyone, in the following code how do I avoid >> d.ThisType "is not a
    type". I cant find the answer anywhere...

    ...please let me know what to do to change my variable so it works in the
    type assertion, I presumed type assertion takes reflect.Value


    type DataType struct {
    ID string
    ThisType reflect.Value
    channel chan interface{}
    }

    func data_channel(d *DataType) {
    for {
    object := (<-d.channel).(d.ThisType)
    switch(d.ID) {
    case "wake_up":
    // insert operational code here
    break
    case "shut_down":
    // insert operational code here
    break
    case "new_instruction":
    // insert operational code here
    break
    }
    }
    }

    --
    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/d/optout.
    --
    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/d/optout.
  • Alex Howard at Aug 23, 2014 at 6:47 pm
    So I learned that you can do a switch on the interface that will give you
    the choice of type

    see below:

    func data_channel(d *DataType) {
      for {
    object := <-d.channel
    switch v := object.(type) {
    case MSG_WakeUp:
      // insert operational code here
      break
    case MSG_ShutDown:
      // insert operational code here
      break
    case MSG_NewInstruction:
      // insert operational code here
      break
    default: fmt.Println(v)
    }
    }
    }

    On Saturday, 23 August 2014 18:41:32 UTC+2, Alex Howard wrote:

    Hello everyone, in the following code how do I avoid >> d.ThisType "is not
    a type". I cant find the answer anywhere...

    ...please let me know what to do to change my variable so it works in the
    type assertion, I presumed type assertion takes reflect.Value


    type DataType struct {
    ID string
    ThisType reflect.Value
    channel chan interface{}
    }

    func data_channel(d *DataType) {
    for {
    object := (<-d.channel).(d.ThisType)
    switch(d.ID) {
    case "wake_up":
    // insert operational code here
    break
    case "shut_down":
    // insert operational code here
    break
    case "new_instruction":
    // insert operational code here
    break
    }
    }
    }
    --
    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/d/optout.
  • Jan Mercl at Aug 23, 2014 at 7:00 pm
    All of the break statements can be dropped.

    -j

    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 23, '14 at 4:41p
activeAug 23, '14 at 7:00p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase