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)
}
必须函数签名相同(函数名,参数和返回值(类型,个数,顺序)都相同),否则会转换失败,无法实现该接口。

最新文章

  1. 深入理解JS的delete
  2. Html书写规范
  3. Mac 安装 eclipse
  4. features block
  5. Android 核心分析 之八Android 启动过程详解
  6. Java的初步认识
  7. JSP内置对象详解
  8. NUMBER_GET_NEXT
  9. uitableviewcell 自适应大小 参考
  10. dom例子
  11. perl $d = encode_utf8($r); $f = decode_json($d)
  12. MySQL连接数实时查看
  13. Python 捕捉traceback异常栈信息
  14. sql日期格式小应用 记录一下
  15. Python全栈之路----函数进阶----装饰器
  16. orcal 程序自动和手动项
  17. 【linux应用】将一个大文件按行拆分成小文件
  18. log4j UdpAppender
  19. GetTextMetrics与GetTextExtent的区别
  20. 【推荐系统论文笔记】Introduction To Recommender Systems: Algorithms and Evaluation

热门文章

  1. 苹果电脑清理软件CleanMyMac X好用吗?
  2. Java基础知识面试题(最详细版)
  3. open()和with open() as的区别
  4. 使用Python开发鸿蒙设备程序(0-初体验)
  5. 这份SpringMVC执行原理笔记,建议做java开发的好好看看,总结的很详细!
  6. Flask端点概念
  7. Java集合【5】-- Collections源码分析
  8. 你知道第一个C语言C++编译器是如何诞生的吗?
  9. 可变长形参,增强for语句
  10. cobbler 装机服务