What's the meaning of interface{}?

后端 未结 6 1359
不思量自难忘°
不思量自难忘° 2020-11-22 10:55

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:

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 11:36

    From the Golang Specifications:

    An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface. The value of an uninitialized variable of interface type is nil.

    A type implements any interface comprising any subset of its methods and may therefore implement several distinct interfaces. For instance, all types implement the empty interface:

    interface{}

    The concepts to graps are:

    1. Everything has a Type. You can define a new type, let's call it T. Let's say now our Type T has 3 methods: A, B, C.
    2. The set of methods specified for a type is called the "interface type". Let's call it in our example: T_interface. Is equal to T_interface = (A, B, C)
    3. You can create an "interface type" by defining the signature of the methods. MyInterface = (A, )
    4. When you specify a variable of type, "interface type", you can assign to it only types which have an interface that is a superset of your interface. That means that all the methods contained in MyInterface have to be contained inside T_interface

    You can deduce that all the "interface types" of all the types are a superset of the empty interface.

提交回复
热议问题