第一次学go语言,测试代码
package main import "fmt" var age int; const sex = 0 func 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]) } }
原文:https://www.cnblogs.com/52-qq/p/9240108.html