3

为何可以这样传递参数

 2 years ago
source link: https://www.v2ex.com/t/848435
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go 编程语言

为何可以这样传递参数

  seers · 22 小时 21 分钟前 · 1075 次点击

初学,有个疑惑的地方,例如

file, err := os.Open("/path/to/file.txt")
scanner := bufio.NewScanner(file)

这里 file 获传入 bufio.NewScanner ,但是文档写的是

func Open(name string) (*File, error)
func NewScanner(r io.Reader) *Scanner

这里接受的是参数是 r io.Reader ,为何 file 可以传过去,是否是因为 file 实现了接口导致的,我在文档里面也没有看到有提及,如果是这样在哪里可以看到哪些参数实现了哪些接口呢,不能要用的时候一个个去断言吧?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK