• go 学习笔记(4) --变量与常量


    “_”   可以理解成一个垃圾桶,我们把值赋给“_”  ,相当于把值丢进垃圾桶,在接下来的程序中运行中不需要这个下划线这个值

    a,b :=1,2 只能用在函数体内

     

    package main
    
    import (
    	"fmt"
    )
    
    const a = iota
    const b = iota //遇到const iota被重置为0
    
    func main() {
    	fmt.Println(a, b)
    }
    

      输出:0 0 

    package main
    
    import (
    	"fmt"
    )
    
    const a = iota
    const (
    	b = iota
    	c = iota //const中每新增一行常量申明,将使iota计数一次,这里变成1
    )
    
    func main() {
    	fmt.Println(a, b, c)
    }
    

      输出:0 0 1

    跳值使用:

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a = iota
    	b = iota
    	_      //iota也会计数一次
    	c = iota //c=3
    )
    
    func main() {
    	fmt.Println(a, b, c)
    }
    

      输出: 0 1 3

    插队使用:

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a = iota
    	b = 3.14
    	c = iota
    )
    
    func main() {
    	fmt.Println(a, b, c)
    }
    

      输出:0 3.14 2

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a = iota * 2
    	b = iota
    	c = iota
    )
    
    func main() {
    	fmt.Println(a, b, c)
    }
    

      输出:0 1 2

    表达式隐式使用法:

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a = iota * 2
    	b   //隐式继承前一个非空表达式iota*2
    	c
    )
    
    func main() {
    	fmt.Println(a, b, c)
    }
    

      输出:0 2 4

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a = iota * 2
    	b = iota * 3
    	c
    	d
    	e = iota
    )
    
    func main() {
    	fmt.Println(a, b, c, d, e)
    }
    

      输出: 0 3 6 9 4

    单行使用法:同一行iota的值是不加的

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a, b = iota, iota + 3  //同一行iota的值是不加的
    	c, d
    	e = iota
    )
    
    func main() {
    	fmt.Println(a, b, c, d, e)
    }
    

      输出:0 3 1 4 2

    package main
    
    import (
    	"fmt"
    )
    
    const (
    	a, b = iota, iota + 3
    	c, d
    	e = iota
    	f
    )
    
    func main() {
    	fmt.Println(a, b, c, d, e, f)
    }
    

      输出:0 3 1 4 2 3

  • 相关阅读:
    netty+springboot+oracle+protobuf 搭建客户端服务端
    netty框架学习记录
    sql查询替换逗号拼接的字符窜
    Node的webpack打包的核心思想就是单页面富应用(SPA)
    Javascript 中的 CJS, AMD, UMD 和 ESM是什么
    springboot读取jar中resource下的文件
    zmq模块的理解和使用二
    问问题
    Java解析kml文件
    练习本
  • 原文地址:https://www.cnblogs.com/saryli/p/11356241.html
Copyright © 2020-2023  润新知