Golang--函数签名相同

条件

必须函数的函数名,参数和返回值(类型,个数,顺序)都相同

验证

例子:

定义函数类型,让相同签名的函数自动实现某个接口。

Negtive:

package interfaceTest

import (
"fmt"
"testing"
) type IntString interface {
test(a int ,s string) (int ,string)
} type likeIntString func(s string ,i int) (int ,string) func (i likeIntString) testaaa(ii int, s string) (int, string) {
return i(s, ii)
} func TestInterface(t *testing.T){
var tt IntString = likeIntString(func(s string, i int) (int, string) {
return i,s
}) i, s := tt.testaaa(7, "dfdf")
fmt.Println(i,s)
}

# Go_test/src/interface/interfaceTest [Go_test/src/interface/interfaceTest.test]

.\interface_test.go:21:6: cannot use likeIntString(func literal) (type likeIntString) as type IntString in assignment:

likeIntString does not implement IntString (missing test method)

.\interface_test.go:25:12: tt.testaaa undefined (type IntString has no field or method testaaa)

Postive

package interfaceTest

import (
"fmt"
"testing"
) type IntString interface {
test(a int ,s string) (int ,string)
} type likeIntString func(s string ,i int) (int ,string) func (i likeIntString) test(ii int, s string) (int, string) {
return i(s, ii)
} func TestInterface(t *testing.T){
var tt IntString = likeIntString(func(s string, i int) (int, string) {
return i,s
}) i, s := tt.test(7, "dfdf")
fmt.Println(i,s)
}
必须函数签名相同(函数名,参数和返回值(类型,个数,顺序)都相同),否则会转换失败,无法实现该接口。

Golang--函数签名相同的更多相关文章

  1. Python: 拷贝函数签名

    使用场景有很多,比如C API在Python下很多都变成了(*args, **kwargs)的参数,这时候可能需要为其添加一个更严格签名来约束参数. 查了许多资料,能有效的拷贝函数签名貌似只能通过动态 ...

  2. GO开发[四]:golang函数

    函数 1.声明语法:func 函数名 (参数列表) [(返回值列表)] {} 2.golang函数特点: a. 不支持重载,一个包不能有两个名字一样的函数 b. 函数是一等公民,函数也是一种类型,一个 ...

  3. Golang 函数

    创建函数 package main import "fmt" //有参数,有返回值 func demo(a int, s string) (int, string) { retur ...

  4. 浅谈python函数签名

    函数签名对象,表示调用函数的方式,即定义了函数的输入和输出. 在Python中,可以使用标准库inspect的一些方法或类,来操作或创建函数签名. 获取函数签名及参数 使用标准库的signature方 ...

  5. golang函数学习笔记

    golang函数特点: a.不支持重载,一个包不能有两个名字一样的函数 b.函数是一等公民,函数也是一种类型,一个函数可以赋值给变量 c.匿名函数 d.多返回值   例子1 func add(a, b ...

  6. swift Equatable 函数签名的测试

    struct Degoo:Equatable { var lex:String var pex:String static func == (left:Degoo, right:Degoo) -> ...

  7. 符号修饰与函数签名、extern “C”(转载)

    转自:http://www.cnblogs.com/monotone/archive/2012/11/16/2773772.html 参考资料: <程序员的自我修养>3.5.3以及3.5. ...

  8. golang函数——可以为类型(包括内置数据类型)定义函数,类似类方法,同时支持多返回值

    不可或缺的函数,在Go中定义函数的方式如下: func (p myType ) funcName ( a, b int , c string ) ( r , s int ) { return } 通过 ...

  9. golang 函数作为类型

    golang 函数作为类型 package main import "fmt" type A func(int, int) func (f A)Serve() { fmt.Prin ...

  10. electron/nodejs实现调用golang函数

    https://www.jianshu.com/p/a3be0d206d4c 思路 golang 支持编译成c shared library, 也就是系统中常见的.so(windows下是dll)后缀 ...

随机推荐

  1. k8S 搭建集群

    k8S 搭建集群1:修改主机名称hostnamectl --static set-hostname masterhostnamectl --static set-hostname node1hostn ...

  2. celery原理与组件

    1.Celery介绍 https://www.cnblogs.com/xiaonq/p/11166235.html#i1 1.1 celery应用举例 Celery 是一个 基于python开发的 分 ...

  3. C语言讲义——内存管理

    动态分配内存 动态分配内存,在堆(heap)中分配. void *malloc(unsigned int num_bytes); 头文件 stdlib.h或malloc.h 向系统申请分配size个字 ...

  4. 通过自定义拦截器优雅的导出Excel并标红的重复数据

    平时我们导入导出Excel的时候如果用poi导出,会发现光设置格式都要很多代码,看起来非常的不优雅.后来业务中遇到了需要导入非常巨大的Excel的需求.如果继续用poi的方式,因为poi把所有exce ...

  5. LeetCode 039 Combination Sum

    题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  6. C语言--计算代码段运行时间

    c语言中有专一包含计算时间函数的头文件,time.h.当我们需要计算某段程序运行的时间时就需要用到time.h包含的clock()函数,在这里介绍一下如何使用这个函数计算代码运行时间. clock函数 ...

  7. Splay树求第k大模板

    今天上午借着休息日得机会手撸了一下模板,终于对着模板调出来了.prev和next占用了std namespace里面的东西,然后报警我上次给关了所以.....就花了3个小时吧. inline加不加无所 ...

  8. 第10.2节 查看导入的Python模块

    在Python中,要查看导入模块,可以使用sys.modules来查看,不过sys包含了所有导入模块包括内建模块,如果需要过滤掉内建模块甚至扩展模块,则需要对sys.modules进行一下过滤. 一. ...

  9. MDX非常规百分比算法-过滤数据后的百分比

    网上有很多关于占比的帖子,基本上都是按照层次结构来做的,比如某个子项占总体的百分比(\all).某个子项占父项的百分比(\parent).某个子项占其祖先的百分比(\ancestor)....等等,如 ...

  10. 解读 authentication.py

    """ Provides various authentication policies. """ import base64 import ...