Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unicode

go

Or is that a myth? Can't see it mentioned in any official documentation (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.

like image 438
Benjamin R Avatar asked Nov 26 '15 22:11

Benjamin R


People also ask

Does Go support Unicode?

With Go being a relatively modern programming language, first released in 2009, it is not unsurprising that it has great support for Unicode strings.

Does Python support Unicode variable names?

Unicode variable namesPython 3 allows many unicode symbols to be used in variable names. Unlike Julia or Swift, which allow any unicode symbol to represent a variable (including emoji) Python 3 restricts variable names to unicode characters that represent characters in written languages.


2 Answers

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.

like image 169
elithrar Avatar answered Sep 21 '22 19:09

elithrar


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: 食=="Meal". 
//In Code  : 食=="Heat Treated Milled Oats,Water,And Sweet Crystal"
type T食 struct{};
like image 32
twitchdotcom slash KANJICODER Avatar answered Sep 19 '22 19:09

twitchdotcom slash KANJICODER