zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    PHP7.27: connect mysql 5.7 using new mysqli
    PHP: Browser, Operating System (OS), Device, and Language Detect
    PHP 在WIN10 下配置
    MySQL chartset
    學習Echart 2.2.7
    The open source JavaScript graphing library that powers Plotly
    D3.js 制作中国地图
    FastReport.Net
    CSS 3D transforms
    SparkCore的调优之开发调优
  • 原文地址:https://www.cnblogs.com/saryli/p/11356241.html
Copyright © 2011-2022 走看看