zoukankan      html  css  js  c++  java
  • 常量

    const Pi = 3.141
    显示类型定义:const b string = “string”
    隐式类型定义:const b = “string”

    没有指定类型的常量被使用时,会根据使用环境来推断其类型。
    var n int
    f(n + 5)//5就是那个没有指定类型的常量
    常量的值必须是在编译时期就能确定的。
    const c1 = 10
    const c2 = getNum()//错误,这个在运行时才知道
    使用内置函数可以,自定义函数就错误。因为编译期间自定义函数属于未知。

    数字类型的常量没有大小和符号,且不会溢出。
    当常量赋值给一个精度过小的数字型变量时,会导致溢出。编译时期发生错误。
    并行赋值:
    const a,b = 1,2
    const str,num = "asdf",15
    const(
    a = 15
    b,c = 10, 20
    )
    常量用作枚举
    const (
    Unknown = 0
    Female = 1
    Male = 2
    )

    const (//iota会自动增加,从0开始
    a = iota
    b = iota
    c = iota
    )

    const (//自动增加,默认都是 = iota
    a = iota
    b
    c
    )
    iota每遇到一次常量就会在使用之前变为0,这种并行赋值不算

  • 相关阅读:
    洛谷 [SDOI2015]约数个数和 解题报告
    multiset-count
    multiset-begin
    multiset-begin
    set-value_comp
    set-value_comp
    multiset-constructors
    multiset-constructors
    set-upper_bound
    set-upper_bound
  • 原文地址:https://www.cnblogs.com/mcmx/p/11380526.html
Copyright © 2011-2022 走看看