'Is it possible to have interface composite literals in Go?

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?



Solution 1:[1]

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

Solution 2:[2]

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".

Solution 3:[3]

  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.

Solution 4:[4]

First, pop Classy CBVs onto your browser bookmark list ...

I'm assuming that you want to use a form, either to get other information from the user, or to allow the user to override the automatically determined value of device. In this case, you want to pass it as the initial value for device to the form

Now, look at CreateView to work out what to subclass. get_initial() looks hopeful, so

def get_initial(self):
    initial = super().get_initial()
    initial['device'] = self.device_type # as per the comment!
    return initial 

You should now see a form with the automatically determined value as the default value

If the intent was to get other fields of the model from the user and to always forcibly insert the automatically determined device_type, you would instead subclass form_valid

def form_valid(form):
    obj = form.save( commit=False)
    obj.device = self.device_type
    obj.save()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 draxil
Solution 3
Solution 4 nigel222