Linux 拨号vps windows公众号手机端

go怎么声明空接口

lewis 8年前 (2017-12-06) 阅读数 7 #程序编程
文章标签 go

在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
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门