Go Concepts - Lexical Structure

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

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

Go programs may omit most of the semicolons at the end of line if the following conditions apply:

  1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream immediately after a line's final token if that token is:
    1. an identifier
    2. an integer, floating-point, imaginary, rune, or string literal
    3. one of the keywords break, continue, fall through, or return
    4. one of the operators and delimiters ++, --, ), ], or }
  2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

For more details, see the specification:

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 "_".

Identifiers can be exported. See exported identifiers.

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: bool, byte, complex64, complex128, float32, float64, int, int8, int16, int32, int64, rune, string, uint, uint8, uint16, uint32, uint64, uintptr and error.

Pre-declared constants: true, false, iota.

Pre-declared functions: append, cap, close, complex, copy, delete, imag, len, make, new, panic, print, println, real, recover. Also see built-in functions.

Pre-declared zero value: nil.

The Blank Identifier

"_" is the blank identifier.

Literals

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. The compiler is always able to infer the literal's type.

Types

The Type System

Constants

Functions

Functions

Operators

Operators

Variables

A variable is a storage location in memory. Go being a statically typed language, a variable always has a specific type. A variable also has a name, which is referred to as variable identifier.

var <variable_identifier> <variable_type_literal> = <initial_value_literal>

Example:

var a string = "blue"
var b = "red"
var c string

Declaring the type is optional if an initial value is specified, the compiler is able to infer the type of the literal, and the variable will be built to have that type.

Assigning a value is optional, if no value is explicitly assigned, the variable is initialized to the type's zero value. However, the type must be specified in this case.

Another form of variable declaration that always assigns an initial value and also infers the variable type based on the literal type is the following:

<variable_identifier> := <initial_value_literal>

Example:

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.

Statements

Statements control execution flow within a function.

for

if

Expressions

Operators combine operands into expressions.

Keywords

range type func . .
package go . . .
defer chan import . .