5 ms·
Go has most of the machinery of objects, but not the encapsulation. There are no private fields in structs.
by Animats 10y ago
Go has most of the machinery of objects, but not the encapsulation. There are no private fields in structs.
- barsonme 10y agoWell, yes kind of. type foo struct { a int // "private" B int // "public" } You cannot specify default values and Go refers to them as "exported" not private, but technically they are "private" in the sense that they can only be accessed via methods.
- kuschku 10y agois a field named "ß" private or public? Or ĸ?
- js2 10y agoAn 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. https://golang.org/ref/spec#Exported_identifiers https://golang.org/ref/spec#Exported_identifiers Both of the characters you asked about are lowercase: http://www.fileformat.info/info/unicode/char/00df/index.htm http://www.fileformat.info/info/unicode/char/00df/index.htm http://www.fileformat.info/info/unicode/char/0138/index.htm http://www.fileformat.info/info/unicode/char/0138/index.htm Here's the full list of Lu characters: http://www.fileformat.info/info/unicode/category/Lu/list.htm http://www.fileformat.info/info/unicode/category/Lu/list.htm
- barsonme 10y agoHere are the rules: https://golang.org/ref/spec#Exported_identifiers https://golang.org/ref/spec#Exported_identifiers An identifier may be exported to permit access to it from another package. An identifier is exported if both: the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and the identifier is declared in the package block or it is a field name or method name. All other identifiers are not exported.
- skj 10y agoDefinitely one or the other, but the confusion means it's best practice to avoid fancy letters for identifiers.
- kuschku 10y agoWell, there's people writing code in other languages. I know in Japan and Germany it's still kinda common for larger companies to build codebases in local languages. Which in turn can lead to these issues.
- gtremper 10y agoTechnically, exported/unexported is different than public/private since its at the package level. You can access unexported fields of a struct from anywhere within the same package (not just from that struct's "methods"), which isn't true of a 'private' field.
- krylon 10y agoIIRC, that is also true for protected members in Java (it has been a while, though, since I have written anything in Java). If that is a concern, you can create a new package for your type.
- david-given 10y agoThere are, actually --- any field which begins with a lower case letter is private, any field which begins with an upper case letter is public. (Don't ask me what happens if the field begins with a letter which has a single form and does not have an explicit upper or lower case.) Privacy is only enforced between packages, so anything in the same package as the struct can see the private fields.
- lloeki 10y ago> Don't ask me what happens if the field begins with a letter which has a single form and does not have an explicit upper or lower case. So, on to the spec: https://golang.org/ref/spec#unicode_letter https://golang.org/ref/spec#unicode_letter > In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of character categories. Go treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo as Unicode letters, and those in the Number category Nd as Unicode digits. Which reads: Lu = Letter, uppercase Ll = Letter, lowercase Lt = Letter, titlecase Lm = Letter, modifier Lo = Letter, other So I suppose (but am not sure) the case you describe falls into Lo.