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

posted on 2019-08-15 10:06  清明-心若淡定  阅读(181)  评论(0编辑  收藏  举报