0

I just want to confirm if my understanding is correct about interface{}{}

Does interface{}{} mean a composite literal of interface type?

So, if I wanted to pass a composite type, lets say []byte as a interface{}, I should assign it to a variable of type interface{}{} and pass in that variable, whereas if I wanted to pass a non composite type, such as a int as a interface{}, I should assign it to a variable of type interface{} and pass in that variable.

Is my understanding correct on this?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
NimaKapoor
  • 119
  • 6
  • 1
    both scenarios, you can use variable with type interface{}. – nipuna Aug 04 '21 at 10:49
  • Related: [What does the double curly brace mean in `[]interface{}{}`](https://stackoverflow.com/questions/57149795/what-does-the-double-curly-brace-mean-in-interface?rq=1) – blackgreen May 23 '22 at 09:08

3 Answers3

2

interface{}{} is invalid syntax.

It is not an empty interface composite literal — there's no such thing. The following don't compile:

var a interface{}{} = "foo" // not a type
b := interface{}{}          // not a value

From the specs Composite Literals:

Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated.

The valid syntax is interface{}, which is the empty interface, i.e. an interface with no name and empty method set.

If you compare it to a named interface, you will see that the extra set of curly braces at the end makes no sense:

type Fooer interface {
    Foo() error
}{} // ???

You can instantiate an empty interface{} by converting nil:

a := (interface{})(nil)

Or you can declare a var of unnamed interface type:

type A struct {}

func (a A) Foo() string {
    return "foo"
}

var a interface{ Foo() string } = A{}

To answer your question:

So, if I wanted to pass a composite type [...]

you can assign any value to a variable of type interface{}, that's about it. Whether the value is composite or not is irrelevant because all types satisfy the empty interface:

var a interface{} = []byte{0x01, 0x02}
var b interface{} = "string_literal"

Playground: https://play.golang.org/p/w-l1dU-6Hb5

blackgreen
  • 34,072
  • 23
  • 111
  • 129
  • So what's the difference between `someVar := make(map[string]interface{})` and `someVar := map[string]interface{}{}` ? – NimaKapoor Aug 05 '21 at 01:10
  • @NimaKapoor the latter is a composite literal of type `map[string]interface{}`. The final `{}` constructs the map. – blackgreen Aug 05 '21 at 04:26
  • OK, if the latter is a composite literal of type `map[string]interface{}`, then what is the former? – NimaKapoor Aug 16 '21 at 08:10
  • @NimaKapoor the former is a type, passed to the [`make`](https://pkg.go.dev/builtin#make) function, as you can see from its signature `func make(t Type, size ...IntegerType) Type`, that takes a type. In both cases `map[string]interface{}` is a type. In the former case the value is initialized with the built-in `make` function, in the latter case it is initialized with a composite literal. – blackgreen Aug 16 '21 at 09:08
0
  1. interface{} type of empty interface.
  2. you assign []byte or int to any empty variable of empty interface (interface{} type) or you can pass it directly to function that exepts interface values.
Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
  • So what's the difference between `someVar := make(map[string]interface{})` and `someVar := map[string]interface{}{}` – NimaKapoor Aug 05 '21 at 01:09
  • there is no difference in this constuctions. both are maps with string as key and interfaces{} as value with a name `someVar`. What you might be missing is `v := map[string]int{}` this is the syntax of declaring a map (usually with a value, but in its example without , hence extra confusing `{}`). – Oleg Butuzov Aug 05 '21 at 02:11
0

The empty interface interface{} essentially says "I don't care". Anything can be passed as an empty interface. So for your examples they all could be stored as the empty interface.

Possibly a more interesting question is why you want to avoid types in the first place, and what you're trying to achieve. But for the purposes of using interface{} you can pass anything to it, even a "nil".

draxil
  • 64
  • 3