常量

const定义在main函数里面就是局部常量
package main

import "fmt"

func add(a int) func(int) int {
    return func(i int) int {
        fmt.Println(&a)
        a += i
        return a
    }
}

func main() {
    const (
        a = 3
        b = iota //初始值是1,不管他上面的值是多少
        c //不写就是默认c = iota
        d    = iota
        e, f = iota, iota //只要不换行,e和f就相等
    )
    fmt.Println(a,b, c, d, e, f)
}
//3 1 2 3 4 4
可以通过.字母大小写来控制常量的访问范围
const (
        A = iota // A可以被别的包访问,下面的不行
        b    //写没写iota都会加一
        c = iota
        d
        e, f = iota, iota //只要不换行就不会多次递增,所以e和f的值是一样的
    )




原文地址:https://www.cnblogs.com/hualou/p/12069700.html