Go Concepts - Lexical Structure: Difference between revisions
Line 37: | Line 37: | ||
''Identifiers'' name program entities, such as constants, variables, types, functions, etc. An identifier is a sequence of one more letter and digits. The first character must be a letter. The letters are unicode letters or underscore "_". | ''Identifiers'' name program entities, such as constants, variables, types, functions, etc. An identifier is a sequence of one more letter and digits. The first character must be a letter. The letters are unicode letters or underscore "_". | ||
==Pre-Declared Identifiers== | |||
Pre-declared identifiers are available by default, without needing to import anything. They are implicitly declared in the ''universe block''. | |||
Pre-declared types: <tt></tt>. | |||
Pre-declared constants: <tt></tt> | |||
Pre-declared functions: <tt></tt>. | |||
Pre-declared zero value: <tt>nil</tt>. | |||
<font color=red> | |||
The blank identifier "_". | The blank identifier "_". | ||
Line 45: | Line 60: | ||
Upper-case letter identifiers | Upper-case letter identifiers | ||
</font> | |||
=Operators= | =Operators= |
Revision as of 18:00, 22 March 2016
Internal
Source Files
Go code goes into source files. They have the .go extension.
Each source file is mandatory declared to belong to a package.
They can then optionally import code from other packages, using the import keyword.
Comments
// to the end of the line
/* Multiline line 1 ... line n */
Whitespace
Whitespace in Go are carriage returns (u+000D), newlines (u+000A), spaces (u+0020) and tabs (u+0009).
Semicolons
TODO https://golang.org/ref/spec#Semicolons
Identifiers
Identifiers name program entities, such as constants, variables, types, functions, etc. An identifier is a sequence of one more letter and digits. The first character must be a letter. The letters are unicode letters or underscore "_".
Pre-Declared Identifiers
Pre-declared identifiers are available by default, without needing to import anything. They are implicitly declared in the universe block.
Pre-declared types: .
Pre-declared constants:
Pre-declared functions: .
Pre-declared zero value: nil.
The blank identifier "_".
Identifiers (not values) can be exported or unexported.
Lower-case letter identifiers
Upper-case letter identifiers
Operators
Expression
Operators combine operands into expressions.
Literal
A literal is a notation for representing a fixed value in source code. Go provides literals for booleans, integers, floating-point values, strings, built-in types such as arrays, slices and maps, user-defined types (structs), functions, etc.
Functions
Statements
- Go Language Specification https://golang.org/ref/spec#Statements
Statements control execution flow within a function.
for
if
Keywords
- Go Specification - Keywords: https://golang.org/ref/spec#Keywords
range | type | func | . | . |
package | go | . | . | . |
defer | chan | import | . | . |
Constants
Variables
Variables are always initialized to the type's zero value.
var <var-name> <type> = <initial-value> var a string = "blah"
<var-name> := <initial-value> a := "blah"
Variable Scopes
Package-level variable
Pass by Value vs Pass by Reference
In Go, all variables are passed by value. Even for pointer variables, since the value of the pointer is a memory address, passing pointer variables is still considered pass by value.
Pointers
Pointer variable.