By user2383973


2014-04-18 06:39:23 8 Comments

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 comments

@Inanc Gumus 2018-06-05 20:58:26

There are already good answers here. Let me add my own too for others who want to understand it intuitively:


Interface

Here's an interface with one method:

type Runner interface {
    Run()
}

So any type that has a Run() method satisfies the Runner interface:

type Program struct {
    /* fields */
}

func (p Program) Run() {
    /* running */
}

func (p Program) Stop() {
    /* stopping */
}
  • Although the Program type has also a Stop method, it still satisfies the Runner interface because all that is needed is to have all of the methods of an interface to satisfy it.

  • So, it has a Run method and it satisfies the Runner interface.


Empty Interface

Here's a named empty interface without any methods:

type Empty interface {
    /* it has no methods */
}

So any type satisfies this interface. Because, no method is needed to satisfy this interface. For example:

// Because, Empty interface has no methods, following types satisfy the Empty interface
var a Empty

a = 5
a = 6.5
a = "hello"

But, does the Program type above satisfy it? Yes:

a = Program{} // ok

interface{} is equal to the Empty interface above.

var b interface{}

// true: a == b

b = a
b = 9
b = "bye"

As you see, there's nothing mysterious about it but it's very easy to abuse. Stay away from it as much as you can.


https://play.golang.org/p/A-vwTddWJ7G

@Minty 2014-12-09 15:41:41

interface{} means you can put value of any type, including your own custom type. All types in Go satisfy an empty interface (interface{} is an empty interface).
In your example, Msg field can have value of any type.

Example:

package main

import (
    "fmt"
)

type Body struct {
    Msg interface{}
}

func main() {
    b := Body{}
    b.Msg = "5"
    fmt.Printf("%#v %T \n", b.Msg, b.Msg) // Output: "5" string
    b.Msg = 5

    fmt.Printf("%#v %T", b.Msg, b.Msg) //Output:  5 int
}

Go Playground

@VonC 2014-04-18 06:54:52

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.

@Mingyu 2014-04-19 05:42:24

What do you mean by "two words of data"? Specifically, what does the "word" mean?

@VonC 2014-04-19 05:49:47

@Mingyu I have completed the answer to illustrate those two words (32-bits points).

@Dan Esparza 2016-03-17 13:17:57

@Mingyu: VonC is referring to a word in the computer architecture sense -- a collection of bits that define a fixed size piece of data. Word size is governed by the processor architecture you're using.

@Victor 2017-12-05 16:58:12

dear all, when you perform a ` type switch, does golang create always a copy of the object? e.g. when you do myObj := myInterface.(ObjType), you create a copy and assign it to myObj, right? so myObj` and the actual object referenced by myInterface are separated values of the same thing. BUT in the other hand if you do myObj := myInterface.(*ObjType), assuming you have pointers, you copy a pointer and, by the ancients laws of the pointers, if you have two pointers at the same thing, using either of them you can makes changes on the underlying sole object.

@VonC 2017-12-05 17:16:40

@Vic please ask a separate question, for all to answer.

@Victor 2017-12-05 17:33:14

thanks @VonC for your reply... the true is that i am tired of getting downpost when i ask things.. people most of the time tell me that i should read the docs... i will recall your suggestion if i feel with will to properly write a post for it... but i really can't think on another way to ask. So thanks anyway and excuse my low will. You are welcome to take a look at this: stackoverflow.com/questions/45577301/… to clarify why i don't like to ask.

@VonC 2017-12-05 18:20:34

@vic no problem, and sorry for your previous bad experience as an asker. It's just that comments are a bad fit for questions and answers.

@Victor 2017-12-05 21:29:26

@VonC really appreciate your feedback on this... is an upvote to the my will. Thanks.

@fabrizioM 2014-04-18 07:06:22

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.

@Denys Séguret 2014-04-18 06:41:49

It's called the empty interface and is implemented by all types, which means you can put anything in the Msg field.

Example :

body := Body{3}
fmt.Printf("%#v\n", body) // -> main.Body{Msg:3}

body = Body{"anything"}
fmt.Printf("%#v\n", body) // -> main.Body{Msg:"anything"}

body = Body{body}
fmt.Printf("%#v\n", body) // -> main.Body{Msg:main.Body{Msg:"anything"}}

This is the logical extension of the fact that a type implements an interface as soon as it has all methods of the interface.

@user2383973 2014-04-18 06:43:45

means it could be an int to user defined structure ??

@Denys Séguret 2014-04-18 06:47:30

You may try there : play.golang.org/p/BbTqJdQvqM

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How GoLang's typecast to interface implemented by a struct and embedded struct works

  • 2018-04-22 05:10:53
  • Humming
  • 112 View
  • 0 Score
  • 2 Answer
  • Tags:   go

4 Answered Questions

[SOLVED] Type converting slices of interfaces in go

1 Answered Questions

[SOLVED] Interface inheritance in non-Struct types

5 Answered Questions

[SOLVED] Meaning of underscore (blank identifier) in Go

  • 2014-06-23 00:30:47
  • Julian
  • 3604 View
  • 20 Score
  • 5 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Copy a struct into a struct which implement an interface

  • 2016-10-27 15:15:59
  • Majonsi
  • 55 View
  • 1 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Interface with unknown properties

  • 2016-02-12 13:05:50
  • Dan Prince
  • 135 View
  • 3 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

2 Answered Questions

[SOLVED] Interfaces and encoding/xml Unmarshal

  • 2014-08-22 14:59:16
  • kwolfe
  • 368 View
  • 0 Score
  • 2 Answer
  • Tags:   xml go

2 Answered Questions

[SOLVED] Embedded Interface

  • 2012-12-16 20:56:23
  • user1612346
  • 613 View
  • 9 Score
  • 2 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] how to get data in interface{} while unmarshaling an XML in Golang?

  • 2014-05-14 12:08:28
  • user2383973
  • 2666 View
  • 1 Score
  • 1 Answer
  • Tags:   go

Sponsored Content