您好,登錄后才能下訂單哦!
小編今天帶大家了解Go接口類型斷言實(shí)例分析,文中知識(shí)點(diǎn)介紹的非常詳細(xì)。覺得有幫助的朋友可以跟著小編一起瀏覽文章的內(nèi)容,希望能夠幫助更多想解決這個(gè)問題的朋友找到問題的答案,下面跟著小編一起深入學(xué)習(xí)“Go接口類型斷言實(shí)例分析”的知識(shí)吧。
Go接口 Interface
定義:Interface類型可以定義?組?法,?來表示?個(gè)對(duì)象的?為特征。 interface不能包含任何變量。
代碼:
interface關(guān)鍵字
定義接口類型,接口是引用類型
是抽象的,具體的類才可以調(diào)用
type Aniaml interface { //定義2個(gè)是行為特征,方法 Eat() Talk() }
定義2個(gè)struct
//只要一個(gè)具體的struct實(shí)現(xiàn)這個(gè)接口的類型的所有方法,也就是具有這個(gè)接口的所有行為特征,都是可以存儲(chǔ)到這個(gè)接口類型變量里面
type Dog struct{ Name string } type Cat struct{ Name string } //添加2個(gè)方法 func (d *Dog) Eat() { fmt.Println(d.Name,"is eat") } func (d1 *Dog) Talk() { fmt.Println(d1.Name,"is 旺旺") } func main(){ //那a animal類型的接口類型的變量,它里面就可以存儲(chǔ)任何這個(gè) 函數(shù)包含著2個(gè)類型 var a Aniaml //dog具體的實(shí)例 var d Dog d.Eat() a = &d a.Eat() }
接口實(shí)現(xiàn)
go中的接口,不需要顯示的實(shí)現(xiàn),只要一個(gè)對(duì)象,實(shí)現(xiàn)了接口類型的所有方法,那么這個(gè)對(duì)象就實(shí)現(xiàn)了這個(gè)接口
記住是實(shí)現(xiàn)所有方法?。。。?/strong>
如果應(yīng)該對(duì)象實(shí)現(xiàn)了多個(gè)interface類型的方法,那么這個(gè)對(duì)象就實(shí)現(xiàn)了多個(gè)接口
測試:type Aniaml interface {
Eat()
Talk()
}func TestOperator() {
//定義一個(gè)接口類型的切片:
var animallist []Aniaml
//實(shí)例化
d := &Dog{
Name:"旺財(cái)",
}
animallist = append(animallist,d)
d1 := &Dog{
Name:"狗子",
}
animallist = append(animallist,d1)
c := &Cat{
Name:"喵喵1",
}
animallist = append(animallist,c)
c1 := &Cat{
Name:"喵喵2",
}
animallist = append(animallist,c1)
for _,v:=range animallist{
v.Eat()
v.Talk()
}
}
空接口,Interface{}
定義:空接口沒有任何方法,所以所以類型都實(shí)現(xiàn)了空接口
package main
import "fmt"
func main() {
//空接口, 既可以存字符串又可以存int
var a interface{}
var b int = 100
a = b
fmt.Println(a)
var c string =" hello "
a = c
fmt.Println(a)
}
類型斷言:
定義:如果我們反向要知道這個(gè)接口變量??實(shí)際存儲(chǔ)的是哪個(gè)類型的對(duì)象可以采?以下?法進(jìn)?轉(zhuǎn)換
var t int
var x interface{}
x = t
y, ok = x.(int) //轉(zhuǎn)成int,帶檢查
列子二:
////a.(type) 獲取變量的類型
switch t := a.(type) {
case *Dog:
t.Eat()
fmt.Printf("t is dog\n")
case *Cat:
t.Eat()
fmt.Printf("t is cat\n")
}
栗子三:
package main import ( "fmt" ) func justify(items ...interface{}) { for index, v := range items { switch v.(type) { case int: fmt.Printf("第 %d 個(gè)參數(shù) is int\n", index) case int32: fmt.Printf("第 %d 個(gè)參數(shù) is int32\n", index) case float32: fmt.Printf("第 %d 個(gè)參數(shù) is float32\n", index) } } } func main(){ var a int var b float32 var c int32 justify(a, b, c) }
判斷一個(gè)變量是否實(shí)現(xiàn)了指定接口:
栗子:
//WeChatPay 是一個(gè)struct類型,
//pay是一個(gè)接口
type Pay interface {
pay(user_id int64,money float64) error
}
type WeChatPay struct {
}
func (w *WeChatPay) pay(user_id int64,money float64) error {
fmt.Println("微信支付!!")
return nil
}
//應(yīng)該先把weChat實(shí)例存到一個(gè)空的接口,然后使用空的interface 是否實(shí)現(xiàn)了這個(gè)接口
weChat := &WeChatPay{}
var tmp interface{} = weChat
_, ok := tmp.(Pay)
if ok {
fmt.Println("weChat is implement Pay interface")
//phone.OpenPay("wechat_pay", weChat)
}
栗子:
使用接口的方法實(shí)現(xiàn)sort功能
###查看系統(tǒng)sort內(nèi)部的接口 是這3個(gè)接口.
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
所以咱只要實(shí)現(xiàn)了這3個(gè)接口,就可以能用sort了
package main
import (
//"sort"
"math/rand"
"fmt"
"sort"
)
type Student struct {
name string
age int
source float32
}
type StudentSlice []*Student
func (p StudentSlice) Len() int {
return len(p)
}
func (p StudentSlice) Less(i,j int) bool {
//fmt.Printf("i,j\n",i,j)
return p[i].age < p[j].age
}
func (p StudentSlice) Swap(i,j int) {
p[i],p[j] = p[j],p[i]
}
func main() {
var studentarr StudentSlice
for i:=0;i<10;i++{
var s = &Student{
name :fmt.Sprintf("任%d",i),
age: rand.Intn(100),
source:rand.Float32() * 100,
}
studentarr = append(studentarr, s)
}
//系統(tǒng)的sort
//sort.Sort(studentarr)
//自定義的sort
site_sort(studentarr)
for i:=0;i<len(studentarr);i++{
fmt.Printf("%#v\n",studentarr[i])
}
}
栗子2:
package main
type SortInterface interface {
Len() int
Less(i,j int) bool
Swap(i,j int)
}
func site_sort(a SortInterface) {
for i :=a.Len() -1 ;i>0;i--{
for j:=0;i<i;j++{
if a.Less(j+1,j){
a.Swap(j,j+1)
}
}
}
}
感謝大家的閱讀,以上就是“Go接口類型斷言實(shí)例分析”的全部內(nèi)容了,學(xué)會(huì)的朋友趕緊操作起來吧。相信億速云小編一定會(huì)給大家?guī)砀鼉?yōu)質(zhì)的文章。謝謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。