Go(golang)实际上允许在变量名中使用Unicode字符吗?

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

Does Go (golang) actually permit unicode characters in variable names?

问题

这是一个神话吗?我在任何官方文档中都没有看到提到(它在那里,呃!)- 当我使用Atom和go-plus包时,它似乎对我不起作用。这真是可惜,因为我正在进行很多数学运算,而且我喜欢数学符号表示法。

更新:我不认为任何数学符号都是晦涩难懂的:‖ ⏋∪
与任何表情符号相比:😀

一个简单的是或否回答,并附上官方来源的引用,对我来说就足够了!

更新:感谢elithrar的回答,我发现'Latin letter lateral click' ǁ 看起来是我想要的'双竖线'的完美类比。

英文:

Or is that a myth? <s>Can't see it mentioned in any official documentation</s> (it's there, D'oh!) – it sure doesn't seem to work for me when using Atom with the go-plus package. It's a shame as I am doing a lot of math operations, and I love mathematic notation.

Update: I don't consider any mathematical symbol as esoteric: ‖ ⏋∪
Compared to any emoji: 😀

A yes or no answer with a reference to an official source would be sufficient for a tick!

Update: Thanks to elithrar's answer, I found 'Latin letter lateral click' ǁ to be a seemingly perfect analogue to 'Double vertical line' that I wanted.

答案1

得分: 8

Go语言支持一些Unicode类别作为标识符。在Go语言规范中,标识符用于命名程序实体,如变量和类型。一个标识符是一个由一个或多个字母和数字组成的序列,其中第一个字符必须是一个字母。

上述类别在规范中定义如下:

  • unicode_letter:Unicode标准中被分类为"Letter"的Unicode代码点。
  • unicode_digit:Unicode标准中被分类为"Decimal Digit"的Unicode代码点。

在Unicode标准6.3的第4.5节"General Category"中定义了一组字符类别。Go语言将那些属于LuLlLtLmLo类别的字符视为Unicode字母,将属于Nd类别的字符视为Unicode数字。

简而言之,一些运算符是被支持的,具体取决于它们的字符类别。一些比较特殊的运算符可能不被支持。

英文:

Go supports some of the Unicode categories as per https://golang.org/ref/spec#Identifiers

> Identifiers name program entities such as variables and types. An identifier is a sequence of one or more letters and digits. The first character in an identifier must be a letter.
>
> identifier = letter { letter | unicode_digit } .

The above classes are defined in the spec as:

> unicode_letter = /* a Unicode code point classified as "Letter" / .
> unicode_digit = /
a Unicode code point classified as "Decimal Digit" */ .
> In The Unicode Standard 6.3, Section 4.5 "General Category" defines a set of character categories. Go treats those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters, and those in category Nd as Unicode digits.

The 'short' answer is that some operators are supported, depending on their character class. The more esoteric/specialist ones won't be.

答案2

得分: 6

是的。我已经用日语和英语混合编程了几个月。我的项目中的所有类型都是两个字符长的。

示例:

type T型 struct{}; //模型      (字面意思:模具/模型)
type T示 struct{}; //视图      (字面意思:展示)
type T師 struct{}; //控制器    (字面意思:主控)

这是“清晰代码”的反面。这个想法是,你的大脑更擅长解析简短而简洁的变量名。

此外,在创建一个新的问题域时,变量名变得难以控制。想象一下,我们正在编程一个新的东西。它是磨碎的燕麦与水混合,甜蜜的结晶物质,然后在一个热箱中经过35分钟的热处理。

变量名:
HeatTreatedMilledOatsWithWaterAndSweetCrystal

是的,我们可以简单地称之为蛋糕。但是还没有人发明蛋糕。

或者我们可以这样:

//字面意思:食==“餐”
//在代码中:食==“热处理的磨碎的燕麦、水和甜蜜的结晶”
type T食 struct{};
英文:

Yes. I've been programming for a few months in a mix of Japanese and English. All types in my project are two characters long.

Example:

type T型 struct{}; //model      (literally: mold/model)
type T示 struct{}; //view       (literally: show      )
type T師 struct{}; //controller (literally: master    )

The antithesis of "clean code". The idea is that your brain is better at
parsing short and concise variable names.

Also, when creating a new problem domain, variable names get out of hand.
Imagine we are programming a new thing. It is milled oats mixed with water, a sweet crystalline substance, and then heat treated in a hot box for 35 minutes.

Variable Name:
HeatTreatedMilledOatsWithWaterAndSweetCrystal

Yeah, we could just call it cake. But no one has invented cake yet.

Or we could just:

//Literally: 食==&quot;Meal&quot;. 
//In Code  : 食==&quot;Heat Treated Milled Oats,Water,And Sweet Crystal&quot;
type T食 struct{};

答案3

得分: 2

为什么不使用ľubozvučná_slovenčina := 3,尽管这对于不了解这种语言的其他程序员来说并不好?

英文:

Why not
ľubozvučná_slovenčina := 3

works, althoght it is not good for other programmers, who don't understand this language.

huangapple
  • 本文由 发表于 2015年11月27日 06:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/33947871.html
匿名

发表评论

匿名网友

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

确定