Go語言程式建立介面
在本文中,我們將學習如何使用 Go 語言建立介面。
介面 - 在 Go 語言中,介面是一種自定義型別,用於指定一個或多個方法簽名。介面是抽象的,即我們不能建立介面的例項,但我們可以為介面型別建立一個變數,並將此變數分配給具有介面所需方法的結構體或類。
語法
type interface_name interface { // method signatures }
示例 1
要定義一個介面,首先我們需要使用 type 關鍵字,它表示我們正在定義一個新型別。我們需要確定介面的名稱,後跟關鍵字 interface,然後需要在此介面內提及方法簽名。
package main import "fmt" // package fmt allows us to print anything on the screen // creating a structure named temp and assigning it a property of count type temp struct { count int } // creating an interface named inter type inter interface { // defining method signatures in the interface getCount() int } // defining and creating a function to the structure that returns an integer value func (t temp) getCount() int { // returning the value of the count return t.count } func main() { // creating a variable to the interface type var struct_intr inter // creating a new instance to the struct and assigning value to it st := temp{98} // Assigning the interface variable to the struct so that we could use the functions defined in // the interface struct_intr = st // accessing the getCount() method from the instance to the struct fmt.Println("Accessing the getCount() method from the st instance \ncount=", st.getCount(), "\n") // priting the count by calling the getCount() method from the struct_inter interface variable fmt.Println("Accessing the getCount() method from the interface variable\ncount=", struct_intr.getCount(), "\n") }
輸出
Accessing the getCount() method from the st instance count= 98 Accessing the getCount() method from the interface variable count= 98
描述
匯入 fmt 包,該包允許我們在螢幕上列印任何內容。
建立一個名為 temp 的結構體,其中 count 為鍵。
建立一個名為 inter 的介面並在其中定義方法簽名。
為結構體定義 getCount() 函式,該函式返回 count 作為整數值。
現在我們需要為介面型別定義一個變數。
將此變數分配給上面定義的結構體,這將允許我們從結構體的例項中使用介面中定義的方法。
建立結構體的例項,併為 count 鍵提供一個整數作為值。
現在,我們可以從介面變數和結構體例項中訪問 getCount() 方法。
透過首先從介面然後從結構體變數呼叫 getCount() 方法來列印 count 的值。
示例 2
使用型別斷言獲取具體型別的返回值,並可以在其上呼叫定義在另一個介面上的方法,但不是滿足介面的一部分。
package main // defining the package’s main // fmt package allows us to print anything on the screen import "fmt" // defining an interface named Polygons and defining method signatures in it type Polygons interface { // defining a method signature named Perimeter() Perimeter() } // defining an interface named Object and defining a method in it too type Object interface { NumberOfSide() } // creating a struct named Pentagon that stores integer values. type Pentagon int // defining a method named Perimeter() to the int struct func (p Pentagon) Perimeter() { // printing the perimeter on the screen fmt.Println("Perimeter of Pentagon", 5*p) } // defining a method named NumberOfSides() to the int struct func (p Pentagon) NumberOfSide() { // getting the number of sides of the pentagon fmt.Println("A pentagon has 5 sides") } func main() { // Giving the integer value to the pentagon struct // further assigning the struct to the interface so that we could use the methods defined in it var p Polygons = Pentagon(50) // using the perimeter method to print the perimeter of the pentagon on the screen p.Perimeter() // assigning the instance to the struct so that we could use the methods present in it. var o Pentagon = p.(Pentagon) // using the NumberOfSides() method to calculate the number of sides of the pentagon o.NumberOfSide() // Giving the integer value to the pentagon struct // further assigning the struct to the interface so that we could use the methods defined in it var obj Object = Pentagon(50) // using the NumberOfSide() method to print the perimeter of the pentagon on the screen obj.NumberOfSide() // printing the perimeter from the obj method var pent Pentagon = obj.(Pentagon) pent.Perimeter() }
輸出
Perimeter of Pentagon 250 A pentagon has 5 sides A pentagon has 5 sides Perimeter of Pentagon 250
描述
匯入 fmt 包,該包允許我們在螢幕上列印任何內容。
建立一個名為 Pentagon 的新結構體,該結構體接受整數值。
建立兩個名為 Polygon 和 object 的介面,並在其中定義方法簽名。
為結構體定義 perimeter() 和 numberOfSides() 函式,它們將分別列印五邊形的周長和邊數。
將整數值賦予五邊形結構體並將結構體分配給介面,以便我們可以使用其中定義的方法。
注意,我們正在使用在 obj 介面中定義的 numberOfSIdes(),而不是在 Polygon 中。
再次,將五邊形 int 賦予 Pentagon 結構體並將其分配給 obj 介面。
同樣,我們正在從 obj 介面上建立的例項中使用 Polygon 介面中定義的方法。
透過這種方式,我們可以呼叫定義在其他介面上的方法,但這些方法不是滿足介面的一部分。