What does 'x.(T)' syntax supposed to mean in Go?
It is called type assertion. Here is how it looks like:
str := value.(string)
It gives you access to the interface’s concrete value. It asserts if the values stored in x
is of type T
and that x
is not nil.
Type assertion can also return two values: the concrete underlying value and a boolean value that check if the assertion is true.
t, ok := x.(T)
Here is some code examples for a much easier references. It is a similar example as the one you can find in A Tour of Go
var i interface{} = "hello"
s := i.(string)
fmt.Println(s)
// Will prints out: hello
s, ok := i.(string)
fmt.Println(s, ok)
// Will prints out: hello, true
f, ok := i.(float64)
fmt.Println(f, ok)
// Will prints out: 0, false
f = i.(float64)
fmt.Println(f)
// Will cause "panic"
// panic: interface conversion: interface {} is string, not float64