导出一个以小写字母开头的函数

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

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语言规范明确规定了这一点:

导出标识符

为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:

  1. 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别“Lu”);
  2. 标识符在包块中声明,或者是字段名或方法名。

所有其他标识符都不会被导出。

英文:

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.

huangapple
  • 本文由 发表于 2017年2月27日 04:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/42473897.html
匿名

发表评论

匿名网友

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

确定