Reputation: 79
This is a slice of interfaces, but why are variables declared so strange: ( ) ( ). What does it mean? What can be instead of nil?
models := []interface{}{
(*entities.User)(nil),
}
Upvotes: 0
Views: 118
Reputation: 38313
(*T)(v)
-- It's a conversion. It converts the untyped nil
to a typed nil
. The type of the nil
after the conversion will be *entities.User
.
The parentheses around the pointer type are necessary for the compiler to know what you mean to do. Without the parentheses it could as well mean that you are trying to dereference the result of the conversion, which is also a valid expression, e.g. *T(v)
means convert v
to T
and dereference the result.
The spec lays it out in more detail: https://go.dev/ref/spec#Conversions
If the type starts with the operator
*
or<-
, or if the type starts with the keywordfunc
and has no result list, it must be parenthesized when necessary to avoid ambiguity:*Point(p) // same as *(Point(p)) (*Point)(p) // p is converted to *Point <-chan int(c) // same as <-(chan int(c)) (<-chan int)(c) // c is converted to <-chan int func()(x) // function signature func() x (func())(x) // x is converted to func() (func() int)(x) // x is converted to func() int func() int(x) // x is converted to func() int (unambiguous)
Upvotes: 2