11 | 接口

接口是用來定義行為的類型。這些被定義的行為不由接口直接實現(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容