[Golang] interfaces
此篇為各筆記之整理,非原創內容,資料來源可見文末參考資料:
- 👍 Interfaces in Go @ medium > rungo
TL;DR
- interface 的概念有點像是的藍圖,先定義某個方法的名稱(function name)、會接收的 參數及型別(list of argument type)、會回傳的值與型別(list of return types)。定義好藍圖之後,並不去管實作的細節,實作的細節會由每個型別自行定義實作(implement)。
// 任何型別,只要符合定義規則的話,就可以被納入 bot interface 中
type bot interface {
// getGreeting 這個函式需要接收兩個參數(string, int),並回傳 (string, error) 才符合入會資格
getGreeting(string, int) (string, error)
// getBotVersion 這個函式需要回傳 float 才符合入會資格
getBotVersion() float64
}