Golang -  Interface

GoLang - 介面 Interface

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對介面 Interface 進行說明

前面我們介紹過,透過 methods 與 struct 來試做一個簡單的物件導向功能,讓 Member 與 User 都能擁有 data() 這個方法,在這裡我們陸續增加一些方法,內容如下:

package main

import "fmt"

type User struct {
    name        string
    age         int
    phone       string
    interest    string
    description string
}

func (tg User) data() string {
    return "this is user:" + tg.name
}

func (tg User) interests() string {
    return "user interest is :" + tg.interest + ", and " + tg.description
}

type Member struct {
    name        string
    balance     int
    age         int
    phone       string
    interest    string
    description string
}

func (tg Member) data() string {
    return "this is member:" + tg.name
}

func (tg Member) interests() string {
    return "my interest is :" + tg.interest + ", and " + tg.description
}

func main() {

    user1 := User{"Adam", 10, "0912345678", "Writing", "writing something"}

    member1 := Member{"Brown", 11, 233, "0912345679", "reading", "reading something"}

    fmt.Println(user1.data()) //output this is user:Adam

    fmt.Println(user1.interests())

    fmt.Println(member1.data()) //output this is member:Brown

    fmt.Println(member1.interests())

}

在這裡我們會看到,在 User 與 Member 都實作了 data() 與 interests() 方法。

Interface

Go 的 Interface 可是做 methods 的組合,以前面的例子,可以用Interface來定義方法接口,並且 interface 可以讓任意的對象實現。

type User struct {
    name        string
    age         int
    phone       string
    interest    string
    description string
}

func (tg User) data() string {
    return "this is user:" + tg.name
}

func (tg User) interests() string {
    return "user interest is :" + tg.interest + ", and " + tg.description
}

type Member struct {
    name        string
    balance     int
    age         int
    phone       string
    interest    string
    description string
}

func (tg Member) data() string {
    return "this is member:" + tg.name
}

func (tg Member) interests() string {
    return "my interest is :" + tg.interest + ", and " + tg.description
}

//Create Interface
type MembersInterface interface {
    data() string
    interests() string
}

func main() {
    user1 := User{"Adam", 10, "0912345678", "Writing", "writing something"}

    var i MembersInterface

    i = user1

    fmt.Println(i.data()) //output this is user:Adam

    fmt.Println(i.interests())//output user interest is :Writing, and writing something

    member1 := Member{"Brown", 11, 233, "0912345679", "reading", "reading something"}

    i = member1

    fmt.Println(i.data()) //output this is member:Brown

    fmt.Println(i.interests())//output my interest is :reading, and reading something

}

Interface 可以單獨應用在非特定型別,可以讓變數能儲存任意型別的值

var mysting interface{}
str := "this is string"
num := 12
//能夠儲存任意型別的值
mystring = str
mystring = num

也可以用在 function 傳入值,例如:

func Example(value interface{}) {  
    switch v := value.(type) {
        case string:
            fmt.Println("string is " + v)
        case int:
            fmt.Println("number is ", v + 2)
    }
}