The constants and operators of the Go language

Source: Internet
Author: User

Use Case 1
//此文件用来练习//常量与运算符的package mainimport "fmt"//定义单个常量const SPARK_NAME = "spark-test001"const NODE_NUM int = 3//下面的方式,只是简写const (    CPU    = 3.4    MEMORY = 1024)//同时定义多个变量const FTP_NAME, ES_NAME, YARN_NAME = "ftp-beijing", "es-beijing", "yarn-shanghai"const (    ftp_cpu, es_cpu, yarn_cpu = 3.4, 5.6, 8.9)func main() {    fmt.Println("spark_name:\t", SPARK_NAME)    fmt.Println("node_num:\t", NODE_NUM)    fmt.Println("cpu:\t",CPU)    fmt.Println("ftp_name:\t", FTP_NAME)    fmt.Println("es_name:\t", ES_NAME)    fmt.Println("yarn_name:\t",YARN_NAME)    fmt.Println("ftp_cpu:\t",ftp_cpu)    fmt.Println("es_cpu:\t", es_cpu)    fmt.Println("yarn_cpu:\t", yarn_cpu)}
Use Case 2
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    a = "A"    //定义常量组时,如果不提供初始化值的话,则表示将使用上行的表达式    //因此,b,d,d的值全是A    b    c    d)func main() {    fmt.Println(a)    fmt.Println(b)    fmt.Println(c)    fmt.Println(d)}
Enumeration Example 1
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    z = "A"    //iota是常量的计数器,从0开始,组中每定义1个常量自动递增1    //通过初始化规则与iota可以达到枚举的效果    //每遇到一个const关键字,iota就会重置为0    b = iota    c    d)func main() {    fmt.Println(z)    fmt.Println(b) //1    fmt.Println(c) //2    fmt.Println(d) //3}
Example 2
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    z = "A"    b = iota    c = iota    d)func main() {    fmt.Println(z)    fmt.Println(b) //1    fmt.Println(c) //2    fmt.Println(d) //3}
Example 3
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    //第一个常量不可省略表达式    Monday    = iota    Tuesday    Wednesday    Thursday    Friday    Saturday    Sunday)func main() {    fmt.Println(Monday)    //0    fmt.Println(Tuesday)   //1    fmt.Println(Wednesday) //2    fmt.Println(Thursday)  //3    fmt.Println(Friday)    //4    fmt.Println(Saturday)  //5    fmt.Println(Sunday)    //6}
Example 4
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    //第一个常量不可省略表达式    Monday    = iota    Tuesday    Wednesday = "we"    Thursday = iota    Friday  = "go"    Saturday = iota    Sunday)func main() {    fmt.Println(Monday)    //0    fmt.Println(Tuesday)   //1    fmt.Println(Wednesday) //we    fmt.Println(Thursday)  //3    fmt.Println(Friday)    //go    fmt.Println(Saturday)  //5    fmt.Println(Sunday)    //6}
Example 4
//此文件用来练习//常量与运算符的package mainimport "fmt"const (    Monday    = "spark"    Tuesday   = "hadoop"    Wednesday = "k8s"    Thursday = iota    Friday    Saturday    Sunday)func main() {    fmt.Println(Monday)    //spark    fmt.Println(Tuesday)   //hadoop    fmt.Println(Wednesday) //k8s    fmt.Println(Thursday)  //3    fmt.Println(Friday)    //4    fmt.Println(Saturday)  //5    fmt.Println(Sunday)    //6}
Operators in the Go language

Shift operator 1, left shift right shift test
//此文件用来练习//常量与运算符的package mainimport "fmt"func main() {    //移位运算符测试    fmt.Println(1 << 1)              //2    fmt.Println(1 << 2)              //4    fmt.Println(1 << 4)              //16    fmt.Println(1 << 10)             //1024    fmt.Println(1 << 10 << 10)       //1048576    fmt.Println(1 << 10 << 10 >> 10) //1024}
2, bitwise operators

A&^b indicates that if a position of B is 1, force the element on a of the corresponding position to be cast to 0

&& The good thing is that after the first failure, there is no subsequent judgment.

Enumeration of computer storage units with constants iota and << operators
//此文件用来练习//目的:结合常量iota与<< 运算符实现计算机存储单位的枚举// byte, KB,MB,GB,TB,PB等等package mainimport "fmt"const(    byte float64 = 1 << (iota * 10)    //说明:    //下面的常量,并没有初始化,就会默认采用上面的表达式    // KB = 1 << (iota * 10), 只是,iota 再不断的增加    KB    MB    GB)func main() {    fmt.Println(byte)    fmt.Println(KB)    fmt.Println(MB)    fmt.Println(GB)}

The constants and operators of the Go language

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.