在Golang中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口。

package main

import "fmt"

type Animal interface {
Fly()
Run()
} type Animal2 interface {
Fly()
} type Bird struct {
} func (bird Bird) Fly() {
fmt.Println("Bird is flying!!!!")
} func (bird Bird) Run() {
fmt.Println("Bird is running!!!!")
} func main() { var animal Animal bird := new(Bird)
animal = bird animal = bird //把类实例直接赋值给接口 animal.Fly()
animal.Run() }
Bird is flying!!!!
Bird is running!!!!

空接口可以接收任何类型的值赋值

package main

import "fmt"

func main() {
var test interface{}
test = fmt.Printf("%T,%v\n", test, test)
test = "abc" fmt.Printf("%T,%v\n", test, test)
test = true fmt.Printf("%T,%v\n", test, test)
test = 312.2 fmt.Printf("%T,%v\n", test, test) }
int,
string,abc
bool,true
float64,312.2

Golang接口简单了解的更多相关文章

  1. 【Golang 接口自动化06】微信支付md5签名计算及其优化

    前言 可能看过我博客的朋友知道我主要是做的支付这一块的测试工作.而我们都知道现在比较流行的支付方式就是微信支付和支付宝支付,当然最近在使用低手续费大力推广的京东金融(已改名为京东数科)以后也可能站到第 ...

  2. Golang设计模式—简单工厂模式(Simple Factory Pattern)

    Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...

  3. Golang 接口与反射知识要点

    目录 Golang 接口与反射知识要点 1. 接口类型变量 2. 类型断言 3. 鸭子类型 4. 反射机制 5. reflect 包 TypeOf().ValueOf() Type().Kind() ...

  4. 【Golang 接口自动化00】为什么要用Golang做自动化?

    为什么使用Golang做自动化 顺应公司的趋势学习了Golang之后,因为没有开发那么多的时间和项目来实践,怕步此前学习Java缺少练习遗忘殆尽的后尘,决定利用工作之余的时间把此前用Python的写的 ...

  5. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  6. 【转载】ID3DXSPRITE接口简单使用

    原文:ID3DXSPRITE接口简单使用 前些日子一直研究DDraw,毕竟是DirectX7的东西了,所以转手用DirectD3D9,用了Surface进行绘图,可是怎么做透明色也都是不行loadfr ...

  7. Golang 接口

    1 接口是什么 Golang中没有像Python.Java拥有类和对象的概念,其封装对象或说明对象是通过接口来实现的.比如谁能够实现什么样的功能,便能够将其抽象化封装. 接口定义了一组方法(抽象方法集 ...

  8. Golang接口(interface)三个特性(译文)

    The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...

  9. Asp.net core与golang web简单对比测试

    最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 v2 内存:16G 电脑有点不给力 操作系统:Centos7 ...

随机推荐

  1. vbox中安装mac系统

    参考: https://www.cnblogs.com/liming2017/p/7566953.html

  2. [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...

  4. httpServletResquire 和httpServletResponse

    package com.bjsxt.servlet.request; import java.io.IOException;import javax.servlet.ServletException; ...

  5. hadoop2.4.1 伪分布

           最终的 /etc/profile :#在文件最后添加       # /etc/profile # System wide environment and startup program ...

  6. sv命令空间 packge

    SV中的module,interface,program,checker,都提供declaration空间,内部定义都local当前的那个scope,相互之间的building block不影响,不识 ...

  7. 学习笔记<1>技术体系结构

    Android的系统架构采用了分层架构的思想,如上图所示.从上层到底层共包括四层,分别是   1.应用程序程序层   2.应用框架层   3.系统库和Android运行时 4.Linux内核.   每 ...

  8. ASP.NET MVC Action返回结果类型【转】

    ASP.NET MVC 目前一共提供了以下几种Action返回结果类型: 1.ActionResult(base) 2.ContentResult 3.EmptyResult 4.HttpUnauth ...

  9. Yii2缓存依赖

  10. css中选项卡的实现

    制作一个简单的选项卡,以供初学者参考:关于css书写的比较粗糙.请见谅 <!DOCTYPE html><html lang="en"><head> ...