go

go怎么声明空接口

小亿
98
2023-10-25 17:17:01
栏目: 编程语言
GO开发者专用服务器,限时0元免费领! 查看>>

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

package main

import "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打印出来。输出结果为:

5
Hello
true

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:go怎么用interface声明一个接口

0
看了该问题的人还看了