Golang Learning Notes 3--constants and operators

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

1. Definition of constants

    • The value of the constant is confirmed at compile time

    • Constants are defined in the same format as variables

    • The right side of the equal sign must be a constant or constant expression

    • A function in a constant expression must be a built-in function

//定义单个常量constint1const"A"const (    "123"    len(text)    20)//同时定义多个变量const1"2""3"const (    "456"len(text2),  10)

2. Initialization rules for constants

    • When defining a constant group, an expression that uses the upstream if the initial value is not provided
var1const (    b  = a    //此时报错,因为全局变量在编译时不能确定值,常量必须是编译时能确定值的或使用内置函数获得的值(eg:len()) )  const (    "A"    b    c    //此时 a,b,c都为"A" )  const (    1"A"    c, d    //a,c为1,b,d为"A"。此时注意下行的常量个数必须与上行数量保持一致 )

3. Enumeration of constants

    • Using the same expression does not mean that you have the same value

    • Iota is a constant counter, starting with 0 and automatically incrementing 1 for each of the 1 constants defined in the group.

    • The effect of enumerations can be achieved by initializing rules with iota

    • Iota resets to 0 for every const keyword encountered

const (    "A"    b    iota    d        //d的值为3,因为在常量组中,定义了4个变量所以iota计数器从0开始计数4次,所以为3)

4. Operators

//优先级从高到低        *  /  %  <<  >>  &  &^ =  -  |  ^     ==  !=  <  <=  >= > && ||

Example:

/*   10 binary: 1011 second  --------- & 0010 = 2 (Two of them are 1 for 1)   | 1111 = 15 (one is 1 is 1)  ^ 1101 = 13 (two only one is 1 is 1)  $^ 0100 = 4 (the second is 1) is 0, otherwise the same as the first)  */  
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.