Go Concepts - Lexical Structure: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 46: Line 46:
Pre-declared '''constants''':  <tt>[[Go Booleans#Boolean_Literals|true]]</tt>, <tt>[[Go Booleans#Boolean_Literals|false]]</tt>, <tt>[[Go iota|iota]]</tt>.
Pre-declared '''constants''':  <tt>[[Go Booleans#Boolean_Literals|true]]</tt>, <tt>[[Go Booleans#Boolean_Literals|false]]</tt>, <tt>[[Go iota|iota]]</tt>.


Pre-declared '''functions''':  <tt>[[go Built-In Functions Appending to and Copying Slices#append.28.29|append]]</tt>, <tt>[[Go Built-In Functions Length and Capacity#cap.28.29|cap]]</tt>, <tt>[[Go Built-In Function close|close]]</tt>, <tt>complex</tt>, <tt>[[go Built-In Functions Appending to and Copying Slices#copy.28.29|copy]]</tt>, <tt>delete</tt>, <tt>imag</tt>, <tt>[[Go Built-In Functions Length and Capacity#len.28.29|len]]</tt>, <tt>[[Go Built-In Function make|make]]</tt>, <tt>[[Go Built-In Function new|new]]</tt>, <tt>panic</tt>, <tt>print</tt>, <tt>println</tt>, <tt>real</tt>, <tt>recover</tt>. Also see [[Go Concepts - Functions#Built-in_Functions|built-in functions]].
Pre-declared '''functions''':  <tt>[[go Built-In Functions Appending to and Copying Slices#append.28.29|append]]</tt>, <tt>[[Go Built-In Functions Length and Capacity#cap.28.29|cap]]</tt>, <tt>[[Go Built-In Function close|close]]</tt>, <tt>complex</tt>, <tt>[[go Built-In Functions Appending to and Copying Slices#copy.28.29|copy]]</tt>, <tt>[[Go Built-In Function delete|delete]]</tt>, <tt>imag</tt>, <tt>[[Go Built-In Functions Length and Capacity#len.28.29|len]]</tt>, <tt>[[Go Built-In Function make|make]]</tt>, <tt>[[Go Built-In Function new|new]]</tt>, <tt>panic</tt>, <tt>print</tt>, <tt>println</tt>, <tt>real</tt>, <tt>recover</tt>. Also see [[Go Concepts - Functions#Built-in_Functions|built-in functions]].


Pre-declared zero value: <tt>nil</tt>.
Pre-declared zero value: <tt>nil</tt>.

Revision as of 19:10, 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: 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 "_".

Identifiers (not values) can be exported or unexported.

Lower-case letter identifiers

Upper-case letter identifiers

Types

The Type System

Constants

Functions

Functions

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.

Statements

Statements control execution flow within a function.

for

if

Keywords

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

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.