golang(go)变量

package main

import "fmt"

func main() {
    // 变量定于的语法格式

    //  常规写法
    var age int
    age = 30
    println(age)
    //30

    //简化
    var name string = "jack"
    println(name)
    //jack

    //类型推断
    var float1 = 13145.20
    fmt.Printf("%f,%T\n", float1, float1)
    //13145.200000,float64

    // 简短写法,只能在函数中使用
    reward := 20
    println(reward)
    //20

    //多个变量常规
    var a, b, c int
    a = 1
    b = 2
    c = 3
    println("a = ", a, ",b = ", b, ",c = ", c)
    //a =  1 ,b =  2 ,c =  3

    var d, e, f int = 5, 6, 7
    println("d = ", d, ",e = ", e, ",f = ", f)
    //d =  5 ,e =  6 ,f =  7

    var m, n = 1, "jack"
    println("m = ", m, ",n = ", n)
    //m =  1 ,n =  jack
    
    var (
        j = 8
        k = 9
    )
    println("j = ", j, ",k = ", k)
    //j =  8 ,k =  9

    /* 格式化
       %d, %f,%s, %T,\n
    */
    var n1, f1, s1 = 510, 3.1415926, "jack"
    fmt.Printf("%d,%T\n", n1, n1)
    fmt.Printf("%.2f,%T\n", f1, f1)
    fmt.Printf("%s,%T\n", s1, s1)
    //510,int
    //3.14,float64
    //jack,string
}

 

posted @ 2022-03-12 16:41  大雾哥  阅读(21)  评论(0编辑  收藏  举报