zoukankan      html  css  js  c++  java
  • go标识符、变量、常量

    标识符

    标识符是用来表示Go中的变量名或者函数名,以字母或_开头。后可跟着字母、数字、 _

    关键字

    关键字是Go语言预先定义好的,有特殊含义的标识符。

     变量

    1. 语法:var identifier type

    举例1:

    var a int 
    var b string 
    var c bool 
    var d int = 8 
    var e string = “hello”

    举例二

    var ( 
     a int //0 
     b string //“” 
     c bool //false 
     d int = 8 // 8 
     e string = “hello” //hello 
    )

    举例 三

    package main
    
    import (
        "fmt"
    )
    
    func main(){
        // 方式一
        /*  
        var a int 
        var b string
        var c bool
        var d float32
        */
    
        // 方式二
    
        var(
            a int 
            b string 
            c bool
            d float32
        )
    
        fmt.Printf("a=%d b=%s  c=%t d=%f
    ", a, b, c, d)
    
        a = 12
        b = "ctz"
        c = true
        d = 10.36
    
        fmt.Printf("a=%d b=%s  c=%t d=%f", a, b, c, d)
    }

    常量

    1. 常量使用const 修饰,代表永远是只读的,不能修改。常量定义时必须给定值
    2. 语法:const identifier [type] = value,其中type可以省略。

    举例一

    const b string = “hello world” 
    const b = “hello world” 
    const Pi = 3.1414926
    const a = 9

    举例二(比较优雅的写法)

    const(
     a = 1 
     b = 2 
     c = 3 
    )

    举例三(比较专业的写法)

    const (
     a = iota 
     b 
     c 
    )
    
    
    const(
     a = 1 << iota 
     b 
     c 
    )

    举例四

    package main
    
    import  "fmt"
    
    func main(){
        /*
        常量 定义时一定要给定值 不给值 报错
        常量一旦定义 不可修改
        */
    
        // 方式一
        // const a int = 10
        // const b = "str"
        
        // 方式二
        const(
            a int =10
            b = "str"
        )
         
        fmt.Printf("a=%d b=%s
    ", a , b)
    
        // 下面这种写法如果后面不给定值 则后面常量的值 和前面的一样
        const (
            c int = 200
            d
            e = 300
            f
        )
    
        fmt.Printf("c=%d d=%d e=%d f=%d 
    ", c, d, e, f)
        //=200 d=200 e=300 f=300
       
        // iota 开始为0 依次向下递增1
        const(
            a1 = iota
            a2
            a3
            a4
        )
    
        fmt.Printf("a1=%d a2=%d a3=%d a4=%d 
    ", a1, a2, a3, a4)
        // a1=0 a2=1 a3=2 a4=3
    
        const(
            b1 = iota
            b2 = iota
            b3 = iota
            b4 = iota
        )
        fmt.Printf("b1=%d b2=%d b3=%d b4=%d 
    ", b1, b2, b3, b4)
        // b1=0 b2=1 b3=2 b4=3
    
    
        const(
            c1 = 1 << iota
            c2
            c3
            c4
        )
        fmt.Printf("c1=%d c2=%d c3=%d c4=%d 
    ", c1, c2, c3, c4)
        //c1=1 c2=2 c3=4 c4=8
        const(
            d1 = 1 << iota
            d2 = 1 << iota
             d3 = 1 << iota
            d4 = 1 << iota
        )
        fmt.Printf("d1=%d d2=%d d3=%d d4=%d 
    ", d1, d2, d3, d4)
        //d1=1 d2=2 d3=4 d4=8
    }

     常量 练习

    package main
    
    import "fmt"
    
    
    const (
        a = iota
        b
        c
        d
        e = 8
        f
        g = iota
        h
        k
    )
    
    
    const (
        A = iota
        B
        C
    )
    
    
    func main(){
        fmt.Printf("a= %d
    ", a)
        fmt.Printf("b= %d
    ", b)
        fmt.Printf("c= %d
    ", c)
        fmt.Printf("d= %d
    ", d)
        fmt.Printf("e= %d
    ", e)
        fmt.Printf("f= %d
    ", f)
        fmt.Printf("g= %d
    ", f)
        fmt.Printf("h= %d
    ", h)
        fmt.Printf("k= %d
    ", k)
        fmt.Printf("A= %d
    ", A)
        fmt.Printf("B= %d
    ", B)
        fmt.Printf("C= %d
    ", C)
    }
    /*
    a= 0
    b= 1
    c= 2
    d= 3
    e= 8
    f= 8
    g= 8
    h= 7
    k= 8
    A= 0
    B= 1
    C= 2
    */
    
    /*
    const() 中 如果 定义了 常量 不给定默认值 那么 该常量的表达式 和 其上一个 表达式是相同的
    iota 在const 中 没经过一层 自增1
    */

     

  • 相关阅读:
    leetcode58. 最后一个单词的长度 🌟
    leetcode53. 最大子序和 🌟
    leetcode38. 报数 🌟
    leetcode35. 搜索插入位置 🌟
    leetcode28. 实现strStr() 🌟
    ⚠️ Python 循环列表删除元素的注意事项
    leetcode27. 移除元素 🌟
    leetcode26. 删除排序数组中的重复项 🌟
    javascript 高阶函数 currying & uncurrying
    javascript 高阶函数 实现 AOP 面向切面编程 Aspect Oriented Programming
  • 原文地址:https://www.cnblogs.com/ctztake/p/10224294.html
Copyright © 2011-2022 走看看