加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 变量声明

发布时间:2020-12-16 09:45:54 所属栏目:大数据 来源:网络整理
导读:In Go, variables are explicitly declared and used by the compiler to e.g. check type-correctness of function calls. package main import "fmt" func main () { var declares 1 or more variables. var a string = "initial" fmt . Println ( a ) You

In Go,variablesare explicitly declared and used by the compiler to e.g. check type-correctness of function calls.

package main
import "fmt"
func main() {

vardeclares 1 or more variables.

var a string = "initial" fmt.Println(a)

You can declare multiple variables at once.

var b, c int = 1, 2 fmt.Println(b, c)

Go will infer the type of initialized variables.

var d = true fmt.Println(d)

Variables declared without a corresponding initialization arezero-valued. For example,the zero value for anintis0.

var e int fmt.Println(e)

The:=syntax is shorthand for declaring and initializing a variable,e.g. forvar f string = "short"in this case.

f := "short" fmt.Println(f) }

$ go run variables.go initial 1 2 true 0 short

In Go,top-level variable assignmentsmustbe prefixed with thevarkeyword. Omitting thevarkeyword is only allowed within blocks.

package main

var toplevel = "Hello world"         // var keyword is required

func F() {
        withinBlock := "Hello world" // var keyword is not required
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读