channel可以是各种数据类型:
(map类型)
(struct类型)
(接口类型)
package main
import "fmt"
type student struct {
name string
}
func main() {
var stuChan chan interface{}
//空interface类型可以是任意类型
stuChan = make(chan interface{}, 10)
stu := student{name:"stu01"}
stuChan <- &stu
var stu01 interface{}
stu01 = <- stuChan
fmt.Println(stu01)
var stu02 *student
//将stu01由interface转换为结构体类型指针
stu02, ok := stu01.(*student)
if !ok {
fmt.Println("不可以转换")
return
}
fmt.Println(stu02)
}
输出:
&{stu01}
&{stu01}
Process finished with exit code 0