类型断言有点像向下转型,接口类型转到具体的实现实例类型上
类型断言是一个使用在接口值上的操作。语法上它看起来像x.(T)被称为断言类型,这里x表示一个接口的类型和T表示一个类型

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. //定义一个接口
  8. type Person interface {
  9. Say()
  10. }
  11.  
  12. //定义一个类型
  13. type Tsh struct {
  14. name string
  15. }
  16.  
  17. //实现接口的方法
  18. func (tsh *Tsh) Say() {
  19. fmt.Println("我是 Tsh类型的方法,我是", tsh.name)
  20. }
  21.  
  22. //测试方法
  23. //传递的参数是Person类型
  24. func test(p Person) {
  25. //注意:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  26. //如果要使用它的实现实例Tsh的name属性,那么就需要类型断言
  27. name := p.(*Tsh).name
  28. fmt.Println(name)
  29. }
  30. func main() {
  31. //实例化
  32. tsh := Tsh{
  33. name: "陶士涵",
  34. }
  35. //传入测试方法
  36. test(&tsh)
  37. }

  

[Go] golang类型断言的更多相关文章

  1. golang 类型断言的学习

    在php中有一个 serialize() 函数 可以把数组序列化成字符串进行存储和传输 如果想反序列化这种字符串,在php中只需要一个简单的unserialize() 函数就可以完成了.但是在gola ...

  2. golang类型断言

    一.介绍 类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言 例子: package main import "fmt" func main(){ ...

  3. [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法

    最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...

  4. Golang的类型断言

    类型断言即判断一个变量是不是某个类型的实例,这个经常用在判断接口的类型,基本的格式: y, ok := x.(type) 上面的语句用于判断变量x是不是type类型,有两种结果: x是type类型的变 ...

  5. Golang之interface(多态,类型断言)

    多态用法 package main //一种事物的多种形态,都可以按照统一的接口进行操作 //多态 import ( "fmt" "math/rand" &qu ...

  6. [Golang学习笔记] 06 程序实体3 类型断言和类型转换

    类型断言: 语法:<目标类型的值>,<布尔参数> := <表达式>.( 目标类型 ) // 安全类型断言<目标类型的值> := <表达式>. ...

  7. golang学习笔记:Interface类型断言详情

    原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case str ...

  8. Go的类型断言解析

    经常地我们对一个接口值的动态类型是不确定的,如方法的形参为接口类型时,此时就需要检验它是否符合我们需要的类型.类型断言是一个使用在接口值上的操作.断言类型的语法:x.(T),这里x表示一个接口的类型, ...

  9. Go语言的类型转换和类型断言

    https://my.oschina.net/chai2010/blog/161418 https://studygolang.com/articles/9335  类型转换.类型断言和类型切换 ht ...

随机推荐

  1. [LeetCode] Binary Gap 二进制间隙

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  2. JavaScript / 本地存储

    cookie 首先让我们先了解一下,什么是cookie,cookie是浏览器提供的一种机制,他将document.cookie的接口提供给JavaScript使其可以对cookie进行控制,但cook ...

  3. Ubuntu环境下配置darknet

    本教程基于Linux物理机进行相关配置,要求物理机中包含N卡且Capbility>=3.0,小于3.0(Fermi架构)只允许配置cuda,不能配置使用Cudnn: 本教程分为: 1.安装NVI ...

  4. mysql5.7忽略大小写问题

    mysql5.7忽略大小写问题 1.1 前言 新安装mysql5.7版本后,linux环境下默认是大小写敏感的. 1.2 忽略大小写敏感步骤 进入mysql配置文件:         vi   /et ...

  5. 链表加bfs求补图联通块

    https://oj.neu.edu.cn/problem/1387 给一个点数N <= 100000, 边 <= 1000000的无向图,求补图的联通块数,以及每个块包含的点数 由于点数 ...

  6. NET Core微服务之路:基于Ocelot的API网关Relay实现--RPC篇

    前言 我们都知道,API网关是工作在应用层上网关程序,为何要这样设计呢,而不是将网关程序直接工作在传输层.或者网络层等等更底层的环境呢?让我们先来简单的了解一下TCP/IP的五层模型.     (图片 ...

  7. The algorithm learning of sort which include Bubblesort,Insertsort,Quicksort and Mergesort.

    Notice : these algorithms achieved by Java. So,let's going to it. firstly, what is Bubblesort? why w ...

  8. [Swift]LeetCode518. 零钱兑换 II | Coin Change 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  9. [Swift]LeetCode680. 验证回文字符串 Ⅱ | Valid Palindrome II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  10. [Swift]LeetCode735. 行星碰撞 | Asteroid Collision

    We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...