英文:
Exporting a function whose name starts with a lower case letter
问题
只是好奇,是否有办法导出以小写字符开头的函数,比如 "print" 或 "start"?
另外一点说明:就像你在处理 JSON 时一样:
type T struct {
FieldA int `json:"field_a"`
FieldB string `json:"field_b,omitempty"`
}
英文:
Just curious, is there a way to export a function whose name starts with a lower case character, like "print" or "start"?
A side note: just like you do with JSON:
type T struct {
FieldA int `json:"field_a"`
FieldB string `json:"field_b,omitempty"`
}
答案1
得分: 5
不,没有。Go语言规范明确规定了这一点:
导出标识符
为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:
- 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别“Lu”);
- 标识符在包块中声明,或者是字段名或方法名。
所有其他标识符都不会被导出。
英文:
No, there is not. The Go language specification specifies this clearly:
> ### Exported identifiers
> An identifier may be exported to permit access to it from another package. An identifier is exported if both:
>
> 1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
> 2. the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论