设为首页 加入收藏

TOP

1.go变量
2018-10-19 15:52:54 】 浏览:110
Tags: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)
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇csugo--基于Beego的中南大学教务.. 下一篇mindoc 在线文档接口系统的 docke..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目