dongruyan4948 2014-04-18 06:39
浏览 87
已采纳

interface {}是什么意思?

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{}
  • 写回答

5条回答 默认 最新

  • doubi7346 2014-04-18 06:54
    关注

    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.

    http://research.swtch.com/gointer2.png

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)