7

Go:变量声明的多种方法与默认值

 3 years ago
source link: https://studygolang.com/articles/32191
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go:变量声明的多种方法与默认值

和喆 · 大约12小时之前 · 40 次点击 · 预计阅读时间 1 分钟 · 不到1分钟之前 开始浏览    

变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念。
变量可以通过变量名访问。
Go 语言变量名由字母、数字、下划线组成,其中首个字符不能为数字。
声明变量的一般形式是使用 var 关键字:

package main

import "fmt"

func main(){
    //声明变量方法1 var identifier type
    var a int = 10
    fmt.Println(a)

    //方法2 name := value
    //使用 := 声明变量时必须要有新的变量产生,不然会报错
    //这种不带声明格式的只能在函数体中出现
    b := "Hello world"
    fmt.Println(b)

    //多变量一起声明
    var o, p, q int = 1, 2, 3
    fmt.Println(o + p + q)

    //变量未初始化值,自动赋予默认值
    var c int
    fmt.Println(c)//int类型值默认值为0

    var d string
    fmt.Println(d)//string默认为""

    var e bool
    fmt.Println(e)//bool默认为false

}

有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:1006366459


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK