• 关于const iota


    1. iota 概述

    iota 常用于const表达式中,并且其值是从0开始的,const声明块中每增加一行iota值自增1.

    使用iota可以简化常量的定义,但其规则需要牢牢记住。

    2. 使用

    2.1 日志模块中使用如下代码

    type Priority int
    const (
        LOG_EMERG Priority = iota   // 0
        LOG_ALERT                   // 1
        LOG_CRIT                    // 2
        LOG_ERR                     // 3
        LOG_WARNING                 // 4
        LOG_NOTICE                  // 5
        LOG_INFO                    // 6
        LOG_DEBUG                   // 7
    )
    

    iota初始值为0,也即LOG_EMERG值为0,下面每个常量递增

    2.2 使用位移

    const (
        mutexLocked = 1 << iota         // 1 * 2^0 = 1
        mutexWoken                      // 1 * 2^1 = 2
        mutexStarving                   // 1 * 2^2 =4     
        mutexWaiterShift = iota         // 3
        starvationThresholdNs = 1e6     // 1000000
    )
    

    运算符规则:

    1. 左移运算符<< 是双目运算符。左移n位就是乘以2的n次方。 其功能把<<左边的运算数的各二进位全部左移若干位,由"<<"右边的数指定移动的位数,高位丢弃,低位补0。
    2. 右移运算符>>是双目运算符。右移n位就是除以2的n次方。 其功能是把">>"左边的运算数的各二进位全部右移若干位,">>"右边的数指定移动的位数。

    2.3 同一行可以赋值多个

    const (
        bit0, mask0 = 1 << iota, 1<<iota - 1  // 1 * 2^0, 1* 2^0 -1
        bit1, mask1 = 1                       // 1 * 2^1, 1* 2^1 -1
        _, _                                  // 虽然没有赋值,但是也占用了iota
        bit3, mask3                           // 1 * 2^3, 1 *2^3 -1
    )
    
    • 第0行的表达式展开即bit0, mask0 = 1 << 0, 1<<0 - 1,所以bit0 == 1,mask0 == 0;
    • 第1行没有指定表达式继承第一行,即bit1, mask1 = 1 << 1, 1<<1 - 1,所以bit1 == 2,mask1 == 1;
    • 第2行没有定义常量
    • 第3行没有指定表达式继承第一行,即bit3, mask3 = 1 << 3, 1<<3 - 1,所以bit0 == 8,mask0 == 7;

    中间可以忽略某一行的赋值,但是依然会占用iota的排序

    3. 规则

    1. iota在const关键字出现时被重置为0
    2. const声明块中每新增一行iota值自增1

    const声明还有个特点,即第一个常量如果指定一个表达式,后续的常量如果没有表达式,则继承上面的表达式。

    4. 编译原理

    const块中每一行在GO中使用spec数据结构描述,spec声明如下:

       // A ValueSpec node represents a constant or variable declaration
        // (ConstSpec or VarSpec production).
        //
        ValueSpec struct {
            Doc     *CommentGroup // associated documentation; or nil
            Names   []*Ident      // value names (len(Names) > 0)
            Type    Expr          // value type; or nil
            Values  []Expr        // initial values; or nil
            Comment *CommentGroup // line comments; or nil
        }
    

    这里只关注ValueSpec.Names, 这个切片中保存了一行中定义的常量,如果一行定义N个常量,那么ValueSpec.Names切片长度即为N。也就是说, 一行可以定义多个常量

    所以编译期间构造常量时的伪算法如下:

    for iota, spec := range ValueSpecs {
        for i, name := range spec.Names {
            obj := NewConst(name, iota...) //此处将iota传入,用于构造常量
            ...
        }
    }
    

    iota实际上是遍历const块的索引,每行中即便多次使用iota,其值也不会递增。

    ♥永远年轻,永远热泪盈眶♥
  • 相关阅读:
    一道微分方程应用题中的“微”妙解答 高等数学
    Do Visual Studio Database Project *.refactorlog Files Belong in Source Control?
    WPF binding not updating the view
    Extend IQueryable<T> Where() as OR instead of AND relationship
    How can I add an item to a IEnumerable<T> collection?
    What are the Navigation Properties in Entity Framework
    Is there a quick way to find all columns in SQL Server 2008 R2 that are encrypted/have encrypted data?
    Cannot convert from an IEnumerable<T> to an ICollection<T>
    Why Navigation Properties are virtual by default in EF
    k8smtu设置不当引发的线上故障
  • 原文地址:https://www.cnblogs.com/failymao/p/14910915.html
Copyright © 2020-2023  润新知