界面{}的含义是什么? [英] What's the meaning of interface{}?

查看:143
本文介绍了界面{}的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉接口并尝试通过 github 执行SOAP请求



我不明白

  Msg界面{} 

在此代码中:

  type Envelope struct {
Body`xml:soap:``
}

类型体struct {
Msg interface {}
}

我在

  fmt.Println 

但不明白 $ b

 界面{} 


解决方案

您可以参考文章如何使用Go中的接口(基于 Russ Cox对接口的描述 ):



< blockquote>

什么是界面?

一个接口是两件事:


  • 它是一组方法, 接口{} 类型,空接口是没有方法的接口。



    由于没有implements关键字,所有类型都至少实现零方法,并且自动满足一个接口,所有类型满足空接口
    这意味着如果您编写的函数将接口{} 值作为参数,那么您可以使用任何值


    (这就是 Msg 代表你的问题:任何值)


    $ b

    func DoSomething(v interface {}){
    // ...
    }




    以下是令人困惑的地方: DoSomething 函数中的



    什么是 v 的类型



    初学者地鼠相信 v 是任何类型,但这是错误的。

    v 不是任何t YPE; 它是界面{} 类型
    $ b

    当将值传递给 DoSomething 函数时,Go运行时将执行类型转换

    (必要时),然后将将值转换为界面{}

    所有值都只有一个值在运行时键入,并且 v 的一个静态类型是 interface {}



    接口值由两个数据字构成


    • 一个字是用于指向该值的基础类型的方法表,
    • 另一个词用于指向该值所保存的实际数据。







    附录:这是Russ的文章是关于接口结构相当完整:
    $ b

    类型Stringer接口{
    String()string
    }




    接口值表示为一个双字pair给出一个指针,指向存储在接口中的类型的信息和一个指向相关数据的指针。

    将b指定给Stringer类型的接口值设置接口值的两个字。



    接口值中的第一个单词指向我称之为接口表或itable (发音为i-table;在运行时源中,C实现名称为Itab)。
    它可以从一些关于所涉及类型的元数据开始,然后成为函数指针。

    请注意,itable对应于接口类型,而不是动态类型
    就我们的示例而言,适用于 Stringer 保存类型Binary列出了用于满足Stringer的方法,它只是 String :Binary的其他方法(获取)不会在 itable 中出现。

    在界面值中的单词指向实际数据,在这种情况下是 b 的副本。

    T他赋值 var s Stringer = b 复制了 b 而不是指向 b ,原因与 var c uint64 = b 复制相同:if b s c 应该具有原始值,而不是新的值。

    存储在接口中的值可能是任意大的,但只有一个字专用于保存接口结构中的值,因此分配会在堆上分配一块内存,并将指针记录在单字槽中。



    I'm new to interfaces and trying to do SOAP request by github

    I don't understand the meaning of

    Msg interface{}
    

    in this code:

    type Envelope struct {
        Body `xml:"soap:"`
    }
    
    type Body struct {
        Msg interface{}
    }
    

    I've observed the same syntax in

    fmt.Println
    

    but don't understand what's being achieved by

    interface{}
    

    解决方案

    You can refer to the article "How to use interfaces in Go" (based on "Russ Cox’s description of interfaces"):

    What is an interface?

    An interface is two things:

    • it is a set of methods,
    • but it is also a type

    The interface{} type, the empty interface is the interface that has no methods.

    Since there is no implements keyword, all types implement at least zero methods, and satisfying an interface is done automatically, all types satisfy the empty interface.
    That means that if you write a function that takes an interface{} value as a parameter, you can supply that function with any value.

    (That is what Msg represents in your question: any value)

    func DoSomething(v interface{}) {
       // ...
    }
    

    Here’s where it gets confusing:

    inside of the DoSomething function, what is v's type?

    Beginner gophers are led to believe that "v is of any type", but that is wrong.
    v is not of any type; it is of interface{} type.

    When passing a value into the DoSomething function, the Go runtime will perform a type conversion (if necessary), and convert the value to an interface{} value.
    All values have exactly one type at runtime, and v's one static type is interface{}.

    An interface value is constructed of two words of data:

    • one word is used to point to a method table for the value’s underlying type,
    • and the other word is used to point to the actual data being held by that value.


    Addendum: This is were Russ's article is quite complete regarding an interface structure:

    type Stringer interface {
        String() string
    }
    

    Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data.
    Assigning b to an interface value of type Stringer sets both words of the interface value.

    The first word in the interface value points at what I call an interface table or itable (pronounced i-table; in the runtime sources, the C implementation name is Itab).
    The itable begins with some metadata about the types involved and then becomes a list of function pointers.
    Note that the itable corresponds to the interface type, not the dynamic type.
    In terms of our example, the itable for Stringer holding type Binary lists the methods used to satisfy Stringer, which is just String: Binary's other methods (Get) make no appearance in the itable.

    The second word in the interface value points at the actual data, in this case a copy of b.
    The assignment var s Stringer = b makes a copy of b rather than point at b for the same reason that var c uint64 = b makes a copy: if b later changes, s and c are supposed to have the original value, not the new one.
    Values stored in interfaces might be arbitrarily large, but only one word is dedicated to holding the value in the interface structure, so the assignment allocates a chunk of memory on the heap and records the pointer in the one-word slot.

    这篇关于界面{}的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆