为什么可以像 http.Dir(“.”) 这样将参数传递给类型?

huangapple go评论67阅读模式
英文:

Why is possible to pass parameters to a type like in http.Dir(".")?

问题

在http包中定义了一个自定义类型(type Dir string),然后添加了一个方法Open(https://golang.org/src/net/http/fs.go#L34)。在其他地方调用时,使用http.Dir("."),就像调用函数或方法一样。有人可以解释一下这里为什么这样做,以及发生了什么吗?

英文:

In http package it is defined a custom type (type Dir string) and then a method Open is added to it (https://golang.org/src/net/http/fs.go#L34). When invoked elsewhere it is done by http.Dir(".") as it were a function or method. Can someone explain me why and what is happening here?

答案1

得分: 2

http.Dir(""."") 是一种类型转换。它将字符串 "." 转换为类型 http.Dir

英文:

http.Dir(".") is a type conversion. It converts the string "." to type http.Dir.

huangapple
  • 本文由 发表于 2017年3月20日 08:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/42894177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定