go牛刀小试:切片/递归/接口

slice

package main

import "fmt"

type Book struct {
	title string
	publisher string
	price int
	subject string
	bookID int
}

func printSlice(x []int) {
	fmt.Printf("len=%d, cap=%d, slice=%v\n", len(x), cap(x),x)
}

func main() {
	fmt.Println("hello!")
	b1 := Book{"War and peace", "China Industry", 32, "Novel", 32092301}
    b2 := Book{"Wireless Communication Principle", "US", 168, "Communication expert",32092302}
    fmt.Println(b1)
    fmt.Println(b2)

    // slice, make(type arr, len, capacity)
    var numbers = make([]int, 3,5 )
    printSlice(numbers)

    num2 := []int{0,1,2,3,4,5,6,7,8,9}
    printSlice(num2)

    var num3 =[]int{0,0,0,0,0,0,0,0,0,0}
    copy(num3, num2)
    printSlice(num3)
    printSlice(num2)

    num3 = append(num2,6,7,8,9)
    printSlice(num3)

   //range, used to iterative array, for, string objects;
   for i,v := range num3 {
   	   fmt.Printf("num3[%d] = %d  ",i,v)
	}
   m1 := map[string]string{"cui":"faminly name","Pengf":"Given name"}
   for k, v := range m1 {
   	   fmt.Printf("\n map[%s] = %s",k, v)
   }

}

  • make(obj, len, capacity) , x = append(obj, element), copy(objTarget, objSource)

result

hello!
{War and peace China Industry 32 Novel 32092301}
{Wireless Communication Principle US 168 Communication expert 32092302}
len=3, cap=5, slice=[0 0 0]
len=10, cap=10, slice=[0 1 2 3 4 5 6 7 8 9]
len=10, cap=10, slice=[0 1 2 3 4 5 6 7 8 9]
len=10, cap=10, slice=[0 1 2 3 4 5 6 7 8 9]
len=14, cap=20, slice=[0 1 2 3 4 5 6 7 8 9 6 7 8 9]
num3[0] = 0 num3[1] = 1 num3[2] = 2 num3[3] = 3 num3[4] = 4 num3[5] = 5 num3[6] = 6 num3[7] = 7 num3[8] = 8 num3[9] = 9 num3[10] = 6 num3[11] = 7 num3[12] = 8 num3[13] = 9
map[cui] = faminly name
map[Pengf] = Given name
Process finished with exit code 0

recursive

package main

import "fmt"

func main() {
	fmt.Println("Fibonacci Number\n")

	for x := 0; x < 10; x++  {
		fmt.Println(" ",fibonacci(x))
	}

	y  := 10
    for y > 0 {
    	fmt.Printf("fibonacci num of %d is %d \n", y, fibonacci(y))
    	y--
	}

	z := [10]int{0,1,2,3,4,5,6,7,8,9}
	for _,v := range z {
		fmt.Printf("%d 的阶乘是 %d \n", v, jieChen(v))
	}
}

func fibonacci(x int) int {
	if x > 1 {
		return fibonacci(x-1) + fibonacci(x-2)
	} else {
		return 1
	}
    return 0
}

func jieChen(x int) int {
	if x < 1 {
		return 1
	}else {
		return jieChen(x-1)*x
	}
}

results:

Fibonacci Number

1
1
2
3
5
8
13
21
34
55
fibonacci num of 10 is 89
fibonacci num of 9 is 55
fibonacci num of 8 is 34
fibonacci num of 7 is 21
fibonacci num of 6 is 13
fibonacci num of 5 is 8
fibonacci num of 4 is 5
fibonacci num of 3 is 3
fibonacci num of 2 is 2
fibonacci num of 1 is 1
0 的阶乘是 1
1 的阶乘是 1
2 的阶乘是 2
3 的阶乘是 6
4 的阶乘是 24
5 的阶乘是 120
6 的阶乘是 720
7 的阶乘是 5040
8 的阶乘是 40320
9 的阶乘是 362880

interface

package main

import "fmt"

type human interface {
	say(string)
}

type Woman struct {
	sex string
}

type Man struct {
	sex string
}
type MiddleSex struct {
	sex string
}

func ( man Man) say() {
	fmt.Println("I am a ", man.sex)
}

func (woman Woman) say() {
	fmt.Println("I am an ", woman.sex)
}

func (middleSex MiddleSex) say() {
	fmt.Println("I am a/an ",middleSex.sex)
}

func main() {
    m := Man{"male"}
    w := Woman{"female"}
    m.say()
    w.say()

    mi := MiddleSex{"not male, not female, middle sex"}
    mi.say()

    mi2 := new(MiddleSex)
    mi2.sex="Keep secret sex"
    mi2.say()

}
// 建立接口(抽象方法包含)、建立结构体、实现接口(方法重载实现了)
//传入参数时候,通过(定义)定义结构体存储数据,通过方法来处理 传递参数;

results

I am a male
I am an female
I am a/an not male, not female, middle sex
I am a/an Keep secret sex

  • 重点是:type name interface{method}
  • 定义实现抽象method的方法;–比java要简洁一些,但是非常类似java的接口;
  • 而 go的type name struct{}神似 c的struct
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页