11|11 | 接口
接口是用来定义行为的类型。这些被定义的行为不由接口直接实现,而是通过方法由用户自定义类型实现
- 断言
v, ok := varI.(T)
如果转换合法,v 是 varI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,ok 是 false,也没有运行时错误发生。
func TestInterface(t *testing.T){
i := 65
v,ok := interface{}(i).(int)
t.Log(v,ok,i)
//v,ok := interface{}(i).(int8)//0,false
//v,ok := interface{}(int8(i)).int(8)//65,true
//v,ok := interface{}(i).(string)//false
}
- 值接收者和指针接收者实现接口
type file interface {
write(msg string) bool
read() string
}type shell struct{
name string
}
func (s shell) write(msg string) bool {
fmt.Print("shell write " + msg)
return true
}func (s shell) read() string {
return "hello world"
}func TestInterface(t *testing.T){
sFile := shell{"test.sh"}
val,ok := interface{}(sFile).(file)
t.Log(val,ok)
val,ok = interface{}(&sFile).(file)
t.Log(val,ok)
}
//result: {test.sh} true
// &{test.sh} true
type file interface {
write(msg string) bool
read() string
}type shell struct{
name string
}
func (s *shell) write(msg string) bool {
fmt.Print("shell write " + msg)
return true
}func (s *shell) read() string {
return "hello world"
}func TestInterface(t *testing.T){
sFile := shell{"test.sh"}
val,ok := interface{}(sFile).(file)
t.Log(val,ok)
val,ok = interface{}(&sFile).(file)
t.Log(val,ok)
}
//result : false
// &{test.sh} true
为什么有这个限制? 下面这句话,我还不能理解
编译器并不是总能自动获得一个值的地址
- 嵌套接口实现接口组合
type file interface {
writeable
read() string
execable
}
type writeable interface {
write(msg string) bool
}
type execable interface{
exec()
}type shell struct{
name string
}
func (s shell) write(msg string) bool {
fmt.Print("shell write " + msg)
return true
}
func (s shell) read() string {
return "hello world"
}
func (s shell) exec() {
fmt.Print("shell exec")
}
func TestInterface(t *testing.T){
sFile := shell{"test.sh"}
val,ok := interface{}(sFile).(execable)
//val,ok := interface{}(sFile).(writeable)
t.Log(val,ok,reflect.TypeOf(sFile))
val,ok = interface{}(sFile).(file)
t.Log(val,ok,reflect.TypeOf(sFile))
}
//result : {test.sh} true test.shell
//{test.sh} truetest.shell
- 空接口
interface{}
空接口相当于void *
func TestInterface(t *testing.T){
var any interface{}
any = 5
t.Log(any,reflect.TypeOf(any))
any = "hello world"
t.Log(any,reflect.TypeOf(any))
any = struct{name string}{"fangle"}
t.Log(any,reflect.TypeOf(any))
switch tp := any.(type) {
case int:
t.Log(tp)
case string:
t.Log(tp)
case bool:
t.Log(tp)
default:
t.Log(tp)
}
}/*result :
5 int
hello world string
{fangle} struct { name string }
{fangle}
*/
空接口的内部实现保存了对象的类型和指针。使用空接口保存一个数据的过程会比直接用数据对应类型的变量保存稍慢。因此在开发中,应在需要的地方使用空接口,而不是在所有地方使用空接口
- 接口与实现者的关系
值实现者
type file interface {
setName(name string)
}
type shell struct{
name string
}
func (s shell) setName(name string){
s.name = name
}func TestInterface(t *testing.T){
sFile := shell{"test.sh"}var fFace file = sFile
//sFace.name = "hello" //报错 undifined name
fFace.setName("hell.sh")
t.Log(fFace,sFile,reflect.TypeOf(fFace))var fFace2 file = &sFile
fFace2.setName("hello.sh")
t.Log(fFace2,sFile,reflect.TypeOf(fFace))
}
//result : {test.sh} {test.sh} test.shell
//&{test.sh} {test.sh} test.shell
【11|11 | 接口】指针实现者
type file interface {
setName(name string)
}
type shell struct{
name string
}
func (s *shell) setName(name string){
s.name = name
}func TestInterface(t *testing.T){
sFile := shell{"test.sh"}
var fFace file = &sFile
//var fFace file = sFile//报错 接口变量 与 接口 属性
fFace.setName("hell.sh")
t.Log(fFace,sFile,reflect.TypeOf(fFace))
}
//result:&{hell.sh} {hell.sh}
// *test.shell
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 我要做大厨
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 爱就是希望你好好活着
- 太平之莲
- 知识
- 叙述作文
- 时间老了
- 清明,是追思、是传承、是感恩。