The constants and operators of the Go language

Source: Internet
Author: User
Tags cas
This is a creation in Article, where the information may have evolved or changed.

Constants and operators

  1. You must assign a value after a constant declaration, and in a constant declaration group, the value can be an upward principle
    Example:
    Const (
    AA1 = 1
    Bb1
    Cc1
    )
    BB1 and CC1 are not assigned, then BB1 follows the upward principle, the value equals Aa1, and then cc1 follows the upward principle, cc1 equals the value of BB1, so that three constants have values.
  2. Enumeration Iota
    Const (
    Aa1 = Iota
    Bb1
    Cc1
    )
    When a constant group uses the Iota enumeration, the member values start from 0 to increment. That means aa1= 0,bb1=1, so what's the bottom?

    Const (
    Cas
    BB1 = Iota
    Cc1
    )
    The result is the same, as long as the iota is used within the group, wherever it is, it is incremented from the first constant of the group.
    It will reset if it encounters a const, for example:
    Const (
    Cas
    BB1 = Iota
    CC1 = Iota
    )
    Const (
    Aa2 = Iota
    Bb2
    Cc2
    )
    There is only one iota in the same group, even if more than one is written,
    Encountered const, equivalent to bounds, iota range only within one const range

  3. Small exercise: Using iota, and constants, and << operators to implement computer units: B,KB,MB,GB representation

    const (B float64 = 1 << (iota * 10)KBMB)定义为float是怕数据溢出,利用iota的自增长以及const组省略值后,后一个值跟随前一个值一样,这里是计算表达式的跟随,因为其中iota变化了,所以在计算时就变化了。实现了要求。
  4. Operator:

    一元:^:数据加一取反,无论正负数。     !:取非二元:^:异或     &:与运算     |:或运算     &^:同样是位运算,第二个数的位如果是1,那么把第一 个数的对应位改为0,如果不是1,那么不变,这样结果是第一个数运算之后的结果。
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.