Demo entry 6742815

go

   

Submitted by anonymous on May 19, 2018 at 09:14
Language: Go. Code size: 745 Bytes.

package main

import "fmt"

// 变量都是在包里面的,没有全局变量,作用域只是在包内部
// 函数外面不可以用 := 赋值
var aa = 3
var ss = "kkk"
var bb = true
// 也可以如下定义变量,省着打很多的var
var (
	a = 3
	s = "kkk"
	b = true
)

func variableZeroValue(){
	var a int
	var s string
	fmt.Printf("%d %q\n", a, s)
}

func variableInitialValue()  {
	var a, b int =3, 4
	var s string="abc"
	fmt.Println(a, b, s)
}

func variableTypeDeduction()  {
	var a, b, c, s= 3, 5, true, "def"
	fmt.Println(a, b, c, s)
}

func variableShorter()  {
	a, b, c, s := 3, 5, true, "def"
	b = 5
	fmt.Println(a, b, c, s)
}

func main()  {
	fmt.Println("hello world")
	variableZeroValue()
	variableInitialValue()
	variableTypeDeduction()
	variableShorter()
	fmt.Println(a, s, b)
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).