1.go变量

package main

import "fmt"

func main() {
	//	Declare variables that are set to their zero value.
	// 0 值
	var a int
	var b string
	var c float64
	var d bool
	fmt.Printf("var a int \t %T [%v]\n", a, a)
	fmt.Printf("var b string \t %T [%v]\n", b, b)
	fmt.Printf("var c float64 \t %T [%v]\n", c, c)
	fmt.Printf("var d bool \t %T [%v]\n", d, d)
	/*

		var a int 	 int [0]
		var b string 	 string []
		var c float64 	 float64 [0]
		var d bool 	 bool [false]

	*/

	fmt.Printf("\n")

	// Declare variables and initialize.
	// Using the short variable declaration operator.
	//	 初始值, 使短声明
	aa := 10
	bb := "hello"
	cc := 3.14159
	dd := true

	fmt.Printf("aa := 10 \t %T [%v]\n", aa, aa)
	fmt.Printf("bb := \"hello\" \t %T [%v]\n", bb, bb)
	fmt.Printf("cc := 3.14159 \t %T [%v]\n", cc, cc)
	fmt.Printf("dd := true \t %T [%v]\n", dd, dd)

	/*
		aa := 10 	 int [10]
		bb := "hello" 	 string [hello]
		cc := 3.14159 	 float64 [3.14159]
		dd := true 	 bool [true]

	*/

	//	Specify type and perform a conversion.
	// 类型转换
	aaa := int32(10)
	fmt.Printf("aaa := int32(10) \t %T [%v]\n", aaa, aaa)
	/*
		aaa := int32(10) 	 int32 [10]
	*/

}

/*
	Zero Values:
	Type Initialized Value
	Boolean false
	Integer 0
	Floating Point 0
	Complex 0i
	String "" (empty string)
	Pointer nil
*/

练习

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0

// Declare three variables that are initialized to their zero value and three
// declared with a literal value. Declare variables of type string, int and
// bool. Display the values of those variables.
//
// Declare a new variable of type float32 and initialize the variable by
// converting the literal value of Pi (3.14).
package main

// main is the entry point for the application.
func main() {

	// Declare variables that are set to their zero value.
	var a1 int
	var b1 string
	var c1 bool

	// Display the value of those variables.
	print("a1:", a1, " b1:", b1, " c1:", c1)

	// Declare variables and initialize.
	// Using the short variable declaration operator.
	a11 := 111
	b11 := "what"
	c11 := true

	// Display the value of those variables.
	println("a11-->", a11, " b11-->", b11, " c11-->", c11)

	// Perform a type conversion.
	Pi := float32(3.14)

	// Display the value of that variable.
	println(Pi)
}

posted on 2018-03-15 23:04  cucy_to  阅读(219)  评论(0编辑  收藏  举报

导航