接口是用來定義行為的類型。這些被定義的行為不由接口直接實現(xiàn),而是通過方法由用戶自定義類型實現(xiàn)
- 斷言
v, ok := varI.(T)
如果轉(zhuǎn)換合法,v 是 varI 轉(zhuǎn)換到類型 T 的值,ok 會是 true;否則 v 是類型 T 的零值,ok 是 false,也沒有運行時錯誤發(fā)生。
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
}
- 值接收者和指針接收者實現(xiàn)接口
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 : <nil> false
// &{test.sh} true
為什么有這個限制? 下面這句話,我還不能理解
編譯器并不是總能自動獲得一個值的地址
- 嵌套接口實現(xiàn)接口組合
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} true test.shell
- 空接口
interface{}
空接口相當(dāng)于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}
*/
空接口的內(nèi)部實現(xiàn)保存了對象的類型和指針。使用空接口保存一個數(shù)據(jù)的過程會比直接用數(shù)據(jù)對應(yīng)類型的變量保存稍慢。因此在開發(fā)中,應(yīng)在需要的地方使用空接口,而不是在所有地方使用空接口
- 接口與實現(xiàn)者的關(guān)系
值實現(xiàn)者
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
指針實現(xiàn)者
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