教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go interface类型转换_Golang系列5 | Interface接口&map类型

go interface类型转换_Golang系列5 | Interface接口&map类型

发布时间:2022-02-16   编辑:jiaochengji.com
教程集为您提供go interface类型转换,Golang系列5 | Interface接口&map类型等资源,欢迎您收藏本站,我们将为您提供最新的go interface类型转换,Golang系列5 | Interface接口&map类型资源

go语言接口(interface),类似其他语言的接口的作用,主要用于定义一组函数(方法)签名,不包括实现。

<h2>1.接口定义</h2>

语法:

<pre class="has"><code>type 接口类型名 interface {
函数签名列表
}</code></pre>

例子:

<pre class="has"><code>// 定义Abser接口类型,里面包含一个Abs函数签名定义,不包括函数实现。
type Abser interface {
Abs() float64
}</code></pre> <h2>2.接口实现</h2>

go语言实现接口,采用的是隐式实现方式,一个类(结构体)实现了某个接口类型,并不会像java之类的语言,显式的通过一个implement关键词说明实现了什么接口。

在go语言中一个类,只要实现了接口中的方法,就认为实现了接口,并不需要显式的声明类实现了什么接口。

例子:

<pre class="has"><code>package mainimport ("fmt""math")// 定义Abser接口类型,里面包含一个Abs函数签名定义,不包括函数实现。
type Abser interface {
Abs() float64
}

// 定义一个MyFloat类型,它是float64的别名
type MyFloat float64

// 给MyFloat类型,定义一个Abs方法,计算绝对值
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}

// 定义Vertex类型结构体
type Vertex struct {
X, Y float64
}

// 为Vertex类型定义Abs方法
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X v.Y*v.Y)
}

func main() {
// 定义Abser接口变量
var abser Abser

// 定义MyFloat类型变量
f := MyFloat(-100.25)

// 因为MyFloat实现了,Abser接口中定义的方法,所以可以赋值给接口类型变量
abser = f

// 通过接口类型调用方法
fmt.Println(abser.Abs())

// 定义Vertex类型变量
v := Vertex{10,20}

// 将Vertex变量赋值给接口类型,这里之所以需要取地址符,因为*Vertex类型实现了接口的Abs方法
abser = &v

// 通过接口类型调用方法
fmt.Println(abser.Abs())
}</code></pre>

运行输出:

<pre class="has"><code>100.25
22.360679774997898</code></pre>

说明:

MyFloat和Vertex并没有显式的说明实现Abser接口,只要实现了Abser接口的方法,就认为实现了Abser接口

<blockquote>

提示:go语言中,可以给类型定义方法(函数),并不是只能给struct类型定义方法。

</blockquote> <h2>3.接口变量默认值</h2>

没有初始化的接口变量,默认值是nil

<pre class="has"><code>var abser Abser
if abser == nil {
fmt.Println("is nil")
}</code></pre> <h2>4.空接口</h2>

包含0个方法的接口,就是空接口。

空接口语法:

<pre class="has"><code>interface{}</code></pre>

空接口类型变量可以存储任意类型的数据,类似java的object类型。

例子:

<pre class="has"><code>package main

import "fmt"

func main() {
// 定义空接口变量
var i interface{}

// 赋值一个Int类型数据给空接口
i = 42

fmt.Println(i)

// 再赋值一个string类型数据给空接口
i = "hello"

fmt.Println(i)
}</code></pre>

运行输出:

<pre class="has"><code>42
hello</code></pre> <h3>4.1.interface类型转换</h3>

例如,当我们将一个int类型的数据,赋值给interface{}类型变量后,怎么将interface{}类型转换回int类型?

转换语法:

<pre class="has"><code>t := i.(T)</code></pre>

i 是interface{}类型变量,T是我们要转换的类型

例子:

<pre class="has"><code>// 定义一个interface{}类型,并初始化为字符串
var i interface{} = "hello"

// 转换成string类型
s := i.(string)</code></pre> <blockquote>

注意:interface{}类型转换,只能转换至真实的类型,通俗的讲就是,当初赋值给interface{}变量的是什么类型数据,就只能还原成什么类型数据。

</blockquote> <h3>4.2.判断Interface类型转换失败</h3>

语法:

<pre class="has"><code>t, ok := i.(T)</code></pre>

通过第二个返回值ok判断是否转换成功

例子:

<pre class="has"><code>var i interface{} = "hello"

s, ok := i.(string)</code></pre> <h3>4.3.interface类型判断</h3>

通过switch语句结合i.(type)语法,判断interface类型。

<pre class="has"><code>package main

import "fmt"

func do(i interface{}) {
// 通过switch判断i的类型
switch v := i.(type) {
case int:
fmt.Println("是int类型,", v)
case string:
fmt.Println("是string类型,", v)
default:
fmt.Println("未知类型,", v)
}
}

func main() {
do(21)
do("hello")
do(true)
}</code></pre>

运行输出:

<pre class="has"><code>是int类型 21
是string类型 hello
未知类型 true</code></pre> <blockquote>

主要: type语法只能用在switch语句中。

</blockquote>

go语言支持map类型,map类型是一种键值(key/value)存储的哈希数据结构,读写数据都是通过key进行操作。

<h2>1.定义map类型</h2>

map类型语法:

<pre class="has"><code>var m map[key类型]value类型</code></pre>

定义一个map类型变量,需要指定map的key是什么数据类型,value(值)是什么数据类型。

例子:

<pre class="has"><code>// key是字符串类型,value是int类型
var m map[string]int

// key是字符串类型,value是string类型
var m2 map[string]string

// key是字符串类型,value是User结构体类型
var m3 map[string]User</code></pre> <h2>2.初始化map</h2>

没有初始化的map变量,默认值是nil,需要通过make初始化map类型变量。

语法:

<pre class="has"><code>make(map[key类型]value类型)</code></pre>

例子:

<pre class="has"><code>// 定义一个map
var m map[string]int

// 初始化map
m = make(map[string]int)</code></pre>

通过短变量声明符,可以快速定义和初始化一个map

<pre class="has"><code>// 不需要预先定义变量m的类型,直接通过短变量声明符(:=),直接定义和初始化变量
m := make(map[string]string)</code></pre> <blockquote>

主要:map必须要先初始化才能使用。

</blockquote> <h2>3.读写数据</h2>

读写数据的操作方式,类似数组,通过 [] 符号引用key的值。

语法:

<pre class="has"><code>m[key]</code></pre>

key是什么类型的数据,就传入什么类型的值,例如:string类型的key, 写法为m["key"], int类型的key, 写法为m[123] 。

例子:

<pre class="has"><code>package main

import "fmt"

func main() {
// 定义并初始化map
m := make(map[string]int)

// 保存一个键值对,将key = tizi的值设置为100
m["tizi"] = 100

// 读取key = tizi 的值, 这里得到的值是100
v := m["tizi"]

fmt.Println(v)
}</code></pre> <h2>4.判断key是否存在</h2>

语法:

<pre class="has"><code>elem, ok = m[key]</code></pre>

第一个返回值elem是key的值,第二个返回值ok表示key是否存在。

例子:

<pre class="has"><code>package main

import "fmt"

func main() {
// 定义并初始化map
m := make(map[string]string)

m["tizi"] = "https://www.tizi365.com"

v, ok := m["tizi"]
// 判断key是否存在
if ok {
fmt.Println("key存在, 值等于", v)
} else {
fmt.Print("key不存在!")
}
}</code></pre>

输出:

<pre class="has"><code>key存在, 值等于 https://www.tizi365.com</code></pre> <h2>5.删除数据</h2>

语法:

<pre class="has"><code>delete(m, key)</code></pre>

删除map中的指定key的数据

例子:

<pre class="has"><code>package main

import "fmt"

func main() {
// 定义并初始化map
m := make(map[string]string)

m["tizi"] = "https://www.tizi365.com"
fmt.Println(m)

// 删除key=tizi的数据
delete(m, "tizi")
fmt.Println(m)
}</code></pre>

输出:

<pre class="has"><code>map[tizi:https://www.tizi365.com]
map[]</code></pre>

到此这篇关于“go interface类型转换_Golang系列5 | Interface接口&amp;map类型”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
go interface类型转换_Golang系列5 | Interface接口&amp;map类型
【Golang】go语言面向接口
2020-10-18Go语言接口
go语言学习-Any类型
golang 初始化并赋值_Golang | 既是接口又是类型,interface是什么神仙用法?
Go基础系列:空接口
关于golang面向接口
14. Go 语言中的类型断言是什么?
Go语言面组合式向对象编程基础总结
第07章 Go语言接口(interface),Golang接口(interface)

[关闭]
~ ~