Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
Got a Golang related question by my friend today, who is learning Golang for the first time. Her question is :
When to use public or private identifier(a.k.a variable or function name) and how to make the identifier public or private?
In order to answer this question, first we need to explore Golang's rule of declaring an identifier.
From http://golang.org/ref/spec#identifier :
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 } .
and from (http://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.
In other words, Golang definition of exported means public type.
So, when to use public or private identifier?
use public(exported) to make that variable/function available from anywhere, other classes and instances of the object.
use private to make your variable/function available in its own class only.
and how to make the identifier public or private ?
See examples :
var aName // private
var BigBro // public (exported)
var 123abc // illegal
func (p *Person) SetEmail(email string) { // public because SetEmail() function starts with upper case
p.email = email
}
func (p Person) email() string { // private because email() function starts with lower case
return p.email
}
if you use Unicode such as Chinese, Korean, Cyrillic or Japanese characters as your identifier - the identifier will be in public mode.
References :
See also : Golang : Dealing with struct's private part
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.6k Golang : Get today's weekday name and calculate target day distance example
+6.4k Golang : Warp text string by number of characters or runes example
+6.3k Golang : Totalize or add-up an array or slice example
+11.4k Get form post value in Go
+10.3k Golang : Allow Cross-Origin Resource Sharing request
+5.2k Golang *File points to a file or directory ?
+17.4k Convert JSON to CSV in Golang
+7.1k Linux : How to fix Brother HL-1110 printing blank page problem
+5.1k Javascript : Change page title to get viewer attention
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+9.5k Golang : Turn string or text file into slice example
+11.4k Golang : Surveillance with web camera and OpenCV