Go Concepts - Lexical Structure: Difference between revisions
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
=Source Files= | =Source Files= | ||
Go code goes into source files. Each source file is declared to belong to a [[Go Concepts - Packages#Overview|package]]. | Go code goes into source files. They have the <tt>.go</tt> extension. Each source file is declared to belong to a [[Go Concepts - Packages#Overview|package]]. | ||
=Comments= | =Comments= |
Revision as of 05:18, 22 March 2016
Internal
Source Files
Go code goes into source files. They have the .go extension. Each source file is declared to belong to a package.
Comments
// to the end of the line
/* Multiline line 1 ... line n */
Operators
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
Whitespace
Newlines, spaces and tabs.
Expression
Operators combine operands into expressions.
Literal
String Literal
Numeric Literal
Array Literal
Slice Literal
Map Literal
Struct Literal
Statements
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.