Go 这是一个int的类型转换器?

Go 这是一个int的类型转换器?,go,Go,我有一本书中的代码示例: Go之路:全面介绍Go编程语言 从中我无法理解某些东西是如何工作的。看看代码: package main import ( "fmt" ) type Any interface{} type EvalFunc func(Any) (Any, Any) func main() { evenFunc := func(state Any) (Any, Any) { os := state.(int) ns := os +

我有一本书中的代码示例:

Go之路:全面介绍Go编程语言

从中我无法理解某些东西是如何工作的。看看代码:

package main

import (
    "fmt"
)

type Any interface{}
type EvalFunc func(Any) (Any, Any)

func main() {
    evenFunc := func(state Any) (Any, Any) {
        os := state.(int)
        ns := os + 2
        return os, ns
    }
    even := BuildLazyIntEvaluator(evenFunc, 0)

    for i := 0; i < 10; i++ {
        fmt.Printf("%vth even: %v\n", i, even())
    }
}

func BuildLazyEvaluator(evalFunc EvalFunc, initState Any) func() Any {
    retValChan := make(chan Any)
    loopFunc := func() {
        var actState Any = initState
        var retVal Any
        for {
            retVal, actState = evalFunc(actState)
            retValChan <- retVal
        }
    }
    retFunc := func() Any {
        return <-retValChan
    }
    go loopFunc()
    return retFunc
}

func BuildLazyIntEvaluator(evalFunc EvalFunc, initState Any) func() int {
    ef := BuildLazyEvaluator(evalFunc, initState)
    return func() int {
        return ef().(int)
    }
}

这里发生了什么事?编译器是否将结果转换为int类型?

这是一个类型断言-请参阅。

任何接口{}的类型可能重复对初学者来说是一种非常糟糕/令人困惑的方式。我认为,将接口{}称为接口{}。
x := ef()     // x is of type Any, which is actually an interface{}
y := x.(int)  // this is a type assertion, if the contents of x are an interface, y will be assigned x's int value, otherwise the runtime will panic.
x := ef()     // x is of type Any, which is actually an interface{}
y := x.(int)  // this is a type assertion, if the contents of x are an interface, y will be assigned x's int value, otherwise the runtime will panic.