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

  • 相关阅读:
    Shipconstructor 2005 破解发布
    活动目录(activate directory)
    上海地铁快成了乞丐天堂
    转载:制造业信息化:计划模拟APS软件驱动敏捷制造
    项目管理的测试版发布
    Introdution RemObject SDK
    怎样在osworkflow中使用BeanShell
    Java class 文件反编译工具 FrontEnd 2.0
    Tribon M3 license keygen
    利用osworkflow实现业务流程
  • 原文地址:https://www.cnblogs.com/saryli/p/11356241.html
Copyright © 2011-2022 走看看