在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go接口實例
Go panic錯誤處理實例
Go命令行參數(shù)實例
Go可變參數(shù)的函數(shù)實例
Go通道同步實例
Go非阻塞通道操作實例
Go指針實例
Go數(shù)字解析實例
Go語言指針
Go超時(timeouts)實例
Go速率限制實例
Go信號實例
Go Base64編碼實例
Go計時器實例
Go命令行標志實例
Go原子計數(shù)器實例
Go語言切片
Go隨機數(shù)實例
Go語言類型轉換
Go排序實例
Go時間格式化/解析實例
Go URL解析實例
Go字符串函數(shù)實例
Go語言常量
Go for循環(huán)語句實例
Go函數(shù)多個返回值實例
Go切片實例
Go行過濾器實例
Go語言接口
Go語言數(shù)組
Go語言變量
Go字符串格式化實例
Go斷續(xù)器實例
Go if/else語句實例
Go通道緩沖實例
Go錯誤實例
Go語言映射
Go執(zhí)行過程實例
Go函數(shù)實例
Go有狀態(tài)的goroutines實例
Go按自定義函數(shù)排序實例
Go語言作用域規(guī)則
Go時代(Epoch)實例
Go變量實例
Go互斥體實例
Go語言范圍(range)
Go程序實例
Go語言入門
Go通道路線實例
Go閉包(匿名函數(shù))實例
Go Select實例
Go通道范圍實例
Go集合函數(shù)實例
Hello World程序實例
Go環(huán)境變量實例
Go語言運算符
Go讀取文件實例
Go延遲(defer)實例
Go SHA1哈希實例
Go語言條件和決策
Go語言錯誤處理
Go通道實例
Go指針實例
Go時間日期實例
Go語言字符串
Go語言循環(huán)
Go語言基礎語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實例
Go語言結構體
Go寫文件實例
Go正則表達式實例
Go JSON實例
Go語言教程
Go關閉通道實例
Go接口實例
Go語言遞歸
Go switch語句實例
Go函數(shù)遞歸實例
Go退出程序實例
Go語言程序結構
Go范圍實例
Go語言函數(shù)
Go工作池實例
Go語言數(shù)據(jù)類型

Go接口實例

Go支持在struct類型上定義的方法。area 方法有一個 * rect類型的接收器。可以為指針或值接收器類型定義方法。這里是一個值接收器的例子。
這里調用struct定義的2個方法。

接口是方法簽名的命名集合。這里是幾何形狀(geometry)的基本接口。
對于這個例子,將在rectcircle類型上實現(xiàn)這個接口。

要在Go中實現(xiàn)一個接口,需要實現(xiàn)接口中的所有方法。 這里在rect上實現(xiàn)geometry的一個實例。

以及 circles 的實現(xiàn)。

如果變量具有接口類型,那么可以調用命名接口中的方法。這里是一個通用 measure() 函數(shù),任何幾何形狀都有這個函數(shù)。

circlerect結構類型都實現(xiàn)了幾何(geometry)接口,因此可以使用這些結構體的實例上調用measure()函數(shù)。

所有的示例代碼,都放在 F:\worksp\golang 目錄下。安裝Go編程環(huán)境請參考:http://www.yiibai.com/go/go_environment.html

structs.go的完整代碼如下所示 -

package main

import "fmt"
import "math"

// Here's a basic interface for geometric shapes.
type geometry interface {
    area() float64
    perim() float64
}

// For our example we'll implement this interface on
// `rect` and `circle` types.
type rect struct {
    width, height float64
}
type circle struct {
    radius float64
}

// To implement an interface in Go, we just need to
// implement all the methods in the interface. Here we
// implement `geometry` on `rect`s.
func (r rect) area() float64 {
    return r.width * r.height
}
func (r rect) perim() float64 {
    return 2*r.width + 2*r.height
}

// The implementation for `circle`s.
func (c circle) area() float64 {
    return math.Pi * c.radius * c.radius
}
func (c circle) perim() float64 {
    return 2 * math.Pi * c.radius
}

// If a variable has an interface type, then we can call
// methods that are in the named interface. Here's a
// generic `measure` function taking advantage of this
// to work on any `geometry`.
func measure(g geometry) {
    fmt.Println(g)
    fmt.Println(g.area())
    fmt.Println(g.perim())
}

func main() {
    r := rect{width: 3, height: 4}
    c := circle{radius: 5}

    // The `circle` and `rect` struct types both
    // implement the `geometry` interface so we can use
    // instances of
    // these structs as arguments to `measure`.
    measure(r)
    measure(c)
}

執(zhí)行上面代碼,將得到以下輸出結果 -

F:\worksp\golang>go run interfaces.go
{3 4}
12
14
{5}
78.53981633974483
31.41592653589793

上一篇:Go讀取文件實例下一篇:Go語言映射