Iteration + casting in go

I have this piece of code that uses an iterator in a list

for x:= range s.faces.Iter(){
    x.Render()
}

as compiler points, x has an interface like {} and there is no (i interface) Render () method defined in my code.

changes to

for x:= range s.faces.Iter(){
    x.(faceTri).Render()
}

compiles because the func (f faceTri) Render () method exists but after this runtime error is executed:

panic: interface transformation: * geometry.faceTri interface, not geometry.faceTri

(geometry is a package)

So can anyone point me to a resource that explains how to use iterators + casting?

+3
source share
1 answer

go, (casts - , .. int → int32).

, . x - *faceTri ( faceTri), x.(*faceTri)

EDIT:

, . go , : interface_with_underlying_type_int.(int64) , int int64

, , idiom -ok

not_interface, ok := some_interface.(some_type)

ok , , , , .

+3

Source: https://habr.com/ru/post/1755000/


All Articles