接口和可变参数

接口 #

描述

type  接口名称 interface {
    method1(参数列表) 返回值列表
    method2(参数列表) 返回值列表
    ...
    methodn(参数列表) 返回值列表
}

简单实现、命令模式

//定义command接口
type Command interface {
	Do()
}

type ScanCommand struct {
	path []string
}

//实现Command接口
func (s ScanCommand)Do()  {
	for _, p := range s.path {
		fmt.Printf("scan path is %s\n",p)
	}
}


type ExecCommand struct {
	Shell string
}

//实现Command接口
func (e ExecCommand)Do()  {
	fmt.Printf("exec.Command %s",e.Shell)
}

//调度器
type Inovker struct {
	Cmd Command
}

func (i *Inovker)SetCommand(cmd Command)  {
	i.Cmd = cmd
}

//实现Command接口
func (i *Inovker)Do()  {
	i.Cmd.Do()
}

func main()  {
	scan := ScanCommand{path:[]string{"e:\\aaa","e:\\bb"}}
	invoker := &Inovker{}
	invoker.SetCommand(scan)
	invoker.Do()
}

空接口 interface #

	var a interface{}
	fmt.Printf("%T",a)

空接口参数类型

var ops = map[string]int{
	"move":0,
	"rename":1,
}

func main()	
	output(ops)
	output("aaaa")
	output(111)
	output(float32(12.33))
	output(complex(12.33,433.33))
}

//空接口,可传递各种类型
func output(a interface{})  {
	fmt.Println(a)
}

可变参数 #


func main() {
    var a interface{}
    fmt.Println(Sum(1,2,3,"aaaa",a))
}

func Sum(args ...interface{}) int {
	s := 0
	for _, arg := range args {
		switch v := arg.(type) {
		case int:
			s += v
		case string:
			s += len(v)
		case interface{}:
			s += 0
		}
	}

	return s
}

…interface{}和[]interface{}

[]interface{}{初始值}//初始值可设置各种基础类型,都为interface{}类型
write([]interface{}{1,2,3,"aaa",complex(12.22,22.22),float32(111.22)})

func write(args ...interface{})  {
	for _, v := range args {
		fmt.Println(v)
	}
}
//指针类型
func main()  {
	var str string
	var i1 int
	var f1 float32
	scan(&str,&i1,&f1)
	fmt.Println(str,i1,f1)
}

#output
string pointer 12 22.22

//可变空接口类型
func scan(args ...interface{})  {
    //发送给filterScan处理,要注意args...解包,类似php的extract
	filterScan(args...)
}


func filterScan(args ...interface{})  {
    //...interface{}可变空接口类型,可以直接传递给[]interface{}使用
	fscan(args)
}


func fscan(args []interface{}) {
	for _, v := range args {
		switch v := v.(type) {
		case *bool:
			*v = true
		case *complex64:
			*v = complex64(complex(12.2, 12.12))
		case *complex128:
			*v = complex128(complex(1232.23, 2323.23))
		case *int:
			*v = int(12)
		case *int8:
			*v = int8(12)
		case *int16:
			*v = int16(232)
		case *int32:
			*v = int32(232)
		case *int64:
			*v = int64(223)
		case *uint:
			*v = uint(1)
		case *uint8:
			*v = uint8(1)
		case *uint16:
			*v = uint16(32)
		case *uint32:
			*v = uint32(333)
		case *uint64:
			*v = uint64(334)
		case *uintptr:
			*v = uintptr(22)
		case *float32:
			*v = float32(22.22)
		case *float64:
			*v = float64(22.33)
		case *string:
			*v = "string pointer"
		case *[]byte:
			*v = []byte("byte pointer")
		}
	}
}

常见错误 #

isli := []int{1,2,3,4}

fmt.Println(isli) //Println参数为可变空接口,不能传递[]int类型参数,要传递[]interface{}类型的

下面转化

func main()  {
	ar := []int{1,2,3,4}
	f := func(a []int) interface{}  //返回空接口类型{
		i := make([]interface{},len(a))
		for k, v := range a {
			i[k] = v
		}
		return i
	}
	
	fmt.Println(f(ar))
}
//空接口类型 输出
[1,2,3,4]


func main()  {
	ar := []int{1,2,3,4}
	f := func(a []int) []interface{} //返回为空接口切片{
		i := make([]interface{},len(a))
		for k, v := range a {
			i[k] = v
		}
		return i
	}
	

	fmt.Println(f(ar)...) //注意传递参数f(ar)...
}

//空接口切片,输出内容
1 2 3 4