go怎么声明空接口

   2025-02-15 2320
核心提示:在Go语言中,可以使用interface{}来声明空接口。下面是一个示例代码:package mainimport fmtfunc main() {var emptyInterface i

在Go语言中,可以使用interface{}来声明空接口。下面是一个示例代码:

package mainimport "fmt"func main() {    var emptyInterface interface{}    emptyInterface = 5    fmt.Println(emptyInterface)    emptyInterface = "Hello"    fmt.Println(emptyInterface)    emptyInterface = true    fmt.Println(emptyInterface)}

在上面的代码中,通过var emptyInterface interface{}声明了一个空接口emptyInterface。空接口可以接受任何类型的值。在示例中,向空接口中分别赋值了一个整数、字符串和布尔值,并通过fmt.Println打印出来。输出结果为:

5Hellotrue

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言