go 基础

...
func init

 

 




一个笔记链接 https://cyent.github.io/golang/method/interface_oo/

一个接口展示链接
https://blog.csdn.net/weixin_34204722/article/details/91929123
package main
 
import (
	"fmt"
)
 
// 薪资计算器接口
type SalaryCalculator interface {
	CalculateSalary() int
}
// 普通挖掘机员工
type Contract struct {
	empId  int
	basicpay int
}
// 有蓝翔技校证的员工
type Permanent struct {
	empId  int
	basicpay int
	jj int // 奖金
}
 
func (p Permanent) CalculateSalary() int {
	return p.basicpay + p.jj
}
 
func (c Contract) CalculateSalary() int {
	return c.basicpay
}
// 总开支
func totalExpense(s []SalaryCalculator) {
	expense := 0
	for _, v := range s {
		expense = expense + v.CalculateSalary()
	}
	fmt.Printf("总开支 $%d", expense)
}
 
func main() {
	pemp1 := Permanent{1,3000,10000}
	pemp2 := Permanent{2, 3000, 20000}
	cemp1 := Contract{3, 3000}
	employees := []SalaryCalculator{pemp1, pemp2, cemp1}
	totalExpense(employees)
}

  





// from https://www.liwenzhou.com/posts/Go/09_function/
defer 执行时机
package main

import (
	"log"
)

func f1() int {
	x := 5
	defer func() {
		x++ //修改的是x,不是返回值
	}()
	return x  //没有在写返回值, return x 相当于 匿名的返回值=x,执行RET时,返回值是5
}

func f2() (x int) {
	defer func() {
		x++
	}()
	return 5   //返回值x=5,defer 改的x,执行RET ,结果是6
}

func f3() (y int) {
	x := 5
	defer func() {
		x++
	}()
	return x   //第一步 返回值 y=x=5, 第二步 defer修改x , 第三步,执行RET
}
func f4() (x int) {
	defer func(x int) {
		x++
		log.Println("x in f4 defer func(x int)=====>",x) //1
	}(x)
	return 5    //第一步 x=5 , 第二步 defer 修改的是形参,不是传给匿名函数的x,第三步执行RET
}

func f5() (x int)  {
	defer func(x int)int {
		x++
		log.Println("in f5 defer x ===>",x)
		return x  //没人接受
	}(x)
	return 5 // 第一步 x=5 , 第二步
}



func main() {
	log.Println(f1())
	log.Println(f2())
	log.Println(f3())
	log.Println(f4())
	log.Println(f5())
}

//2021/02/06 23:07:21 5
//2021/02/06 23:07:21 6
//2021/02/06 23:07:21 5
//2021/02/06 23:07:21 x in f4 defer func(x int)=====> 1
//2021/02/06 23:07:21 5
//2021/02/06 23:07:21 in f5 defer x ===> 1
//2021/02/06 23:07:21 5

  


函数
1,带返回值

 

 

 修改函数行为

package main

import "log"

//修改了函数的行为
func adder() func(int) int  {
	var x = 100
	return func(y int) int {
		x = x + y
		return x
	}
}

func add(x, y int) int {
	return x+y
}

func modadd(f func(int, int) int, m, n int) int {
	var x = 10
	return add(m,n)+x
}


func main()  {
	ret := adder()
	ret2 := ret(200)
	log.Println(ret2)
	log.Println(modadd(add,1,2))
}
//2021/02/06 23:49:35 300
//2021/02/07 00:01:09 13

  

 //顺序输出

 

package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	wg := &sync.WaitGroup{}
	ch1 := make(chan int)
	ch2 := make(chan int)

	wg.Add(2)
	go say1(wg, ch1, ch2)
	go say(wg, ch2, ch1)

	wg.Wait()
	time.Sleep(1 * time.Second)
}

func say(wg *sync.WaitGroup, ch2 chan int, ch1 chan int) {
	defer wg.Done()
	for i := 1; i <= 5; i++ {
		ch2 <- 2*i - 1
		fmt.Println("gorouting say",<-ch1)
	}
}

func say1(wg *sync.WaitGroup, ch1 chan int, ch2 chan int) {
	defer wg.Done()
	for i := 1; i <= 5; i++ {
		fmt.Println("gorouting say1",<-ch2)
		ch1 <- 2 * i
	}
}

  

 //三个goroutin 顺序打印

package main

import (
	"fmt"
	"sync"
)

func main() {
	wg := &sync.WaitGroup{}
	ch1 := make(chan int)
	ch2 := make(chan int)
	ch3 := make(chan int)

	wg.Add(3)
	//go say1(wg, ch1, ch2)
	//go say(wg, ch2, ch1)

	go say1(wg,ch2, ch3)
	go say2(wg,ch3, ch1)
	go say3(wg,ch1, ch2)

	wg.Wait()
	//time.Sleep(20 * time.Second)
}

func say1(wg *sync.WaitGroup,ch2 chan int, ch3 chan int) {
	defer wg.Done()
	for i := 1; i <= 5; i++ {
		ch2 <- i
		fmt.Println("gorouting say1", <-ch3)
	}
}

func say2(wg *sync.WaitGroup,ch3 chan int, ch1 chan int) {
	defer wg.Done()
	for i := 1; i <= 5; i++ {
		fmt.Println("gorouting say2", <-ch1)
		ch3 <-  i
	}
}

func say3(wg *sync.WaitGroup,ch1 chan int, ch2 chan int) {
	defer wg.Done()
	for i := 1; i <= 5; i++ {
		fmt.Println("gorouting say3", <-ch2)
		ch1 <-  i
	}
}

 

stuct 中嵌套 interface

目的==> 目的是当前结构体实例可以用所有实现了该接口的其他结构体来初始化(即使他们的属性不完全一致)

 

posted @ 2021-02-06 23:10  voh99800  阅读(66)  评论(0编辑  收藏  举报