func calc(a, b int) (sum int, sub int) { //返回值名字可以写在 返回类型前面 sum = a + b sub = a - b return } func calc_v1(b ...int) int { //多个参数类型一样,可以用... sum := 0 for i := 0; i < len(b); i++ { sum += b[i] } return sum }
func calc_v2(a int, b ...int) int {
sum := a
for i := 0; i < len(b); i++ {
sum = sum + b[i]
}
return sum
}
-------defer
func testDefer1() { defer fmt.Println("hello") defer fmt.Println("hellosecond") fmt.Println("aaaa") fmt.Println("bbbb") //打印的时候 先打印正常的逻辑,再打印最后一个defer,再打印倒数第二个 } func testDefer2() { for i := 0; i < 5; i++ { defer fmt.Printf("i=%d\n", i) } fmt.Printf("哈哈") //先打印哈哈 再打印 4,3,2,1 } func testDefer3() { var i int = 0 defer fmt.Printf("defer i=%d", i) //这个时候 defer已经被赋值了,所以是0 i = 1000 fmt.Printf("i=%d", i) }
1.函数介绍
2. 多返回值和可变参数
3. defer语句
4. 内置函数介绍
如何连续的⼀系列参数的类型是⼀样,前⾯的类型可以不写,
多返回值和可变参数
func justify(n int) bool { if n <= 1 { return false } for i := 2; i < n; i++ { if n%i == 0 { return false } } return true //返回为true则为质数 } func example1() { for i := 1; i <= 100; i++ { if justify(i) == true { fmt.Printf("%d is 质数\n", i) } } } func is_shuixianhua(n int) bool { first := n % 10 second := (n / 10) % 10 third := (n / 100) % 10 // fmt.Println(first, second, third) sum := first*first*first + second*second*second + third*third*third if sum == n { return true } return false } func example2() { for i := 100; i <= 999; i++ { if is_shuixianhua(i) == true { fmt.Printf("[%d]is 水仙花\n", i) } } }