英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论