Go Concepts - Lexical Structure: Difference between revisions
Line 82: | Line 82: | ||
Statements control execution flow within a [[Go Concepts - Functions#Function_Definition|function]]. | Statements control execution flow within a [[Go Concepts - Functions#Function_Definition|function]]. | ||
==for== | ==for== |
Revision as of 06:32, 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
Types, functions, constants, variables, interfaces.
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
String Literal
A string literal is a sequence of characters of a definite length.
Numeric Literal
Array Literal
Slice Literal
Map Literal
Struct Literal
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.