douliao5550
2018-03-23 11:06
浏览 33
已采纳

将接口转换为结构

type SipField interface {
    Info() (id, name, defaultValue string, length int)
}

type Field string

func (f *Field) Get() string {
    return string(*f)
}

func (f *Field) Set(s string) {
    *f = Field(s)
}

type CommandID Field

func (cid *CommandID) Info() (id, name, defaultValue string, length int) {
    return "", "command ID", "", 2
}

type Language Field

func (l *Language) Info() (id, name, defaultValue string, length int) 
{
    return "", "language", "019", 3
}

func InitField(f interface{}, val string) error {
    sipField, ok := f.(SipField)
    if !ok {
        return errors.New("InitField: require a SipField")
    }
    _, _, defaultValue, length := sipField.Info()
    field, ok := f.(*Field)
    if !ok {
     return errors.New("InitField: require a *Field")
    }
    return nil
}

How should I do for converting interface{} to Field(CommandID, Language...) in InitField() function? I try to directly type assert by

field, ok := f.(*Field)

but it not working.I have tried to use unsafe.Pointer but failed also.

图片转代码服务由CSDN问答提供 功能建议

  type SipField接口{
 Info()(ID,名称,defaultValue字符串,长度int)\  n} 
 
type字段字符串
 
func(f * Field)Get()字符串{
返回字符串(* f)
} 
 
func(f * Field)Set(s string){
  * f =字段
} 
 
type CommandID字段
 
func(cid * CommandID)Info()(标识,名称,defaultValue字符串,长度int){
返回“”,“命令ID”  ,“”,2 
} 
 
类型语言字段
 
func(l * Language)Info()(id,名称,defaultValue字符串,长度int)
 {
返回“”,“ language”,  “ 019”,3 
} 
 
func InitField(f interface {},val string)error {
 sipField,ok:= f。(SipField)
 if!ok {
返回错误。New(“  InitField:需要一个SipField“)
} 
 _,_,defaultValue,长度:= sipField.Info()
字段,确定:= f。(* Field)
,如果!ok {
返回错误。  New(“ InitField:require * Field”)
} 
返回nil 
} 
   
 
 

如何转换 interface {} Field(Com indField()函数中的mandID,语言...)? 我尝试直接通过

  field键入assert,好:= f。(* Field)
   
 
 

但是 它不起作用。我尝试使用unsafe.Pointer但也失败了。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhan8430 2018-03-23 11:35
    已采纳

    Have a look at Type assertions chapter in Go reference. It states:

    x.(T)

    More precisely, if T is not an interface type, x.(T) asserts that the dynamic type of x is identical to the type T.

    Types CommandID and Field are not identical as described in Type identity.

    A defined type is always different from any other type.

    Both types CommandId and Fields are defined as descirbed in Type definitions.

    A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.

    TypeDef = identifier Type .

    You can only do

    field, ok := f.(*CommandID)
    

    or

    field, ok := f.(*Language)
    

    As @mkopriva mentioned in the comment, you can do type conversion later to *Field but this it does not seem to be your goal.

    Other solution is to introduce a Field interface with Set and Get methods. Then you will need to provide an implementation for every implementing type.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题