標籤:常量 運算子
用例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)}
用例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)}
枚舉舉例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}
舉例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}
舉例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}
舉例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}
舉例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}
Go語言中的運算子
移位元運算符1、左移 右移 測試
//此檔案用來練習//常量與運算子的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、位元運算符
A&^B 表示 如果B的某個位置是1的話,就強制將對應位置的A上元素,強制轉換為0
&& 好處就是第一個失敗後,就不會進行後面的判斷了
結合常量iota與<< 運算子實現電腦儲存單位的枚舉
//此檔案用來練習//目的:結合常量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)}
Go語言之常量與運算子