dragonhong641016 2019-01-08 23:22
浏览 411

为什么不能在分配中使用(func(string)类型)作为func(interface {})类型

Please first have a look at the code below.

package main

import "fmt"

type InterfaceFunc func(interface{})
type StringFunc func(string)

func stringFunc(s string) {
    fmt.Printf("%v", s)
}

func interfaceFunc(i interface{}) {
    fmt.Printf("%v", i)
}

func main() {
    var i = interfaceFunc
    var s = stringFunc

    i = s // I would like someone to explain why this can't be done exactly.
}

Run at https://play.golang.org/p/16cE4O3eb95

Why an InterfaceFunc can't hold a StringFunc while an interface{} can hold a string.

  • 写回答

1条回答 默认 最新

  • doutang7414 2019-01-09 05:33
    关注

    You can not do s = i or i = s, and the reason is both functions are of different type (different signatures), you can not just assign one type with another in golang.

    Also type InterfaceFunc func(interface{}) type StringFunc func(string) are sitting there doing nothing.

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制