標籤:color 函數 focus idt 五行 [] show UNC pen
第一次學go語言,測試代碼
package mainimport "fmt"var age int;const sex = 0func init() { fmt.Println("Mr_man") age = 9 fmt.Println(age)}func main() { fmt.Println("hello world",sex) xxx() fmt.Println("=========================") const LENGTH int = 10 const WIDTH int = 5 var area int const a, b, c = 1, false, "str" area = LENGTH * WIDTH fmt.Printf("面積為:%d", area) println() println(a, b, c) println("========================") compute() compare() focuss() maxx(2, 3) ShowYangHuiTriangle() mei()}func xxx() { fmt.Println("Mr_zhang") fmt.Println("我是在main函數中被觸發調用的") const ( a = iota b c d = "ha" e f = 100 g h = iota i ) fmt.Println(a, b, c, d, e, f, g, h, i)}func compute() { var a int = 21 var b int = 10 var c int c = a + b fmt.Printf("第一行 - C的值為: %d \n", c) c = a - b fmt.Printf("第二行 - C的值為: %d \n", c) c = a * b fmt.Printf("第三行 - C的值為: %d \n", c) c = a / b fmt.Printf("第四行 - C的值為: %d \n", c) c = a % b fmt.Printf("第五行 - C的值為: %d \n", c) a++ fmt.Printf("第六行 - C的值為: %d \n", a) a = 21 a-- fmt.Printf("第七行 -C的值為: %d \n", a)}func compare() { var a int = 21 var b int = 10 if(a == b) { fmt.Printf("第一行 - a 等於 b \n") } else { fmt.Printf("第一行 - a 不等於 b \n") } if (a < b) { fmt.Printf("第二行 -a 小於 b") } else { fmt.Printf("第二行 - a 不小於 b \n") } if (a > b) { fmt.Printf("第三行 - a 大於 b \n") } else { fmt.Printf("第三行 - a 不大於 b \n") } a = 5 b = 20 if (a <= b) { fmt.Printf("第四行 - a 小於等於 b \n") } if (b >= a) { fmt.Printf("第五行 - b 大於等於 a \n") }}func focuss() { var a int = 4 var b int32 var c float32 var ptr *int fmt.Printf("第一行 - a 變數的類型為 = %T \n", a) fmt.Printf("第二行 - b 變數類型為 = %T \n", b) fmt.Printf("第三行 - c 變數類型為 = %T \n", c) ptr = &a fmt.Printf("a 的值為 %d \n", a) fmt.Printf("*ptr 為 %d \n", *ptr)}func maxx(num1, num2 int) int { fmt.Println("===========>maxx") var result int if (num1 > num2) { result = num1 } else { result = num2 } fmt.Println(result) return result}//行數const LINES int = 10// 楊輝三角func ShowYangHuiTriangle() { nums := []int{} for i := 0; i < LINES; i++ { //補空白 for j := 0; j < (LINES - i); j++ { fmt.Print(" ") } for j := 0; j < (i + 1); j++ { var length = len(nums) var value int if j == 0 || j == i { value = 1 } else { value = nums[length-i] + nums[length-i-1] } nums = append(nums, value) fmt.Print(value, " ") } fmt.Println("") }}func mei() { var n [10] int var i, j int for i = 0; i < 10; i++{ n[i] = i + 100 } for j = 0; j < 10; j++{ fmt.Printf("Element[%d] = %d\n", j, n[j]) }}
Go語言測試代碼