本文主要介绍func

func

image-20210912155140848

匿名

image-20210912155159245

image-20210912155357595

不定项参数

image-20210912155503668

image-20210912155553301

闭包

image-20210912160019267

延迟调用

image-20210912160128895

image-20210913201643325

image-20210913201702759

接口

package main

import "fmt"

type Animal interface {
	Eat()
	Run()
}

type Cat struct {
	Name string
	Sex  bool
}
type Dog struct {
	Name string
}

func (d Dog) Run() {
	fmt.Println(d.Name, "开始跑")
}
func (d Dog) Eat() {
	fmt.Println(d.Name, "开始吃")
}
func (d Cat) Run() {
	fmt.Println(d.Name, "开始跑")
}
func (d Cat) Eat() {
	fmt.Println(d.Name, "开始吃")
}
func main() {
	//var a Animal
	//c := Cat{
	//	Name: "tom",
	//	Sex:  true,
	//}
	//
	//a = c
	//a.Eat()
	//a.Run()
	//fmt.Println(c)
	c := Cat{
		Name: "tom",
		Sex:  true,
	}
	MYfunc(c)

}

func MYfunc(a Animal) {
	a.Run()
	a.Eat()

}

package main

import "fmt"

type Animal interface {
	Eat()
	Run()
}

type Cat struct {
	Name string
	Sex  bool
}
type Dog struct {
	Name string
}

func (d Dog) Run() {
	fmt.Println(d.Name, "开始跑")
}
func (d Dog) Eat() {
	fmt.Println(d.Name, "开始吃")
}
func (d Cat) Run() {
	fmt.Println(d.Name, "开始跑")
}
func (d Cat) Eat() {
	fmt.Println(d.Name, "开始吃")
}

var L Animal

func main() {
	//var a Animal
	//c := Cat{
	//	Name: "tom",
	//	Sex:  true,
	//}
	//
	//a = c
	//a.Eat()
	//a.Run()
	//fmt.Println(c)
	c := Cat{
		Name: "tom",
		Sex:  true,
	}
	MYfunc(c)
	L.Run()

}

func MYfunc(a Animal) {
	L = a

}

并发神器

断言