Go (programming language), aka Golang
Go is a statically typed, compiled high-level programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is syntactically similar to C, but also has memory safety, garbage collection, structural typing, and CSP-style concurrency. It is often referred to as Golang because of its former domain name, golang.org, but its proper name is Go.
— Wikipedia
A Tour of Go
Some notes which I made during the a Tour of Go.
Every Go program is made up of packages.
Programs start running in package main.
By convention, the package name is the same as the last element of the import
path. For instance, the math/rand
package comprises files that begin with the
statement ==package rand
==.
How to import packages?
Better to use “factored” import statement (good style):
But you can also write multiple import statements, like:
Which names can be exported in Go?
In Go, a name is exported if it begins with a capital letter. For example,
Pizza is an exported name, as is Pi, which is exported from the math package.
Write the simplest Go function which return sum of two arguments (x, y)
How to shorten this declaration x int, y int
?::x, y int
How many results function in Go can return?
A function can return any number of results.
Can we use named return values in Go?
Yes, if so, they are treated as variables defined at the top of the function.
These names should be used to document the meaning of the return values.
A return statement without arguments returns the named return values. This is
known as a “naked” return.
How to declare list of variables in Go?
Need to use the var
statement (as in function argument lists), the type is
last. A var
statement can be at package or function level.
Can we initialize variable values and declare them in same time?
Yes, a var declaration can include initializers, one per variable.
If an initializer is present, the type can be omitted; the variable will take
the type of the initializer.
What :=
is used for?
This is short assignment statement. Can be used in place of a var declaration
with implicit type.
Where every statement begins with a keyword (var
, func
, and so on) and so
the :=
construct is not available?
Outside a function.
Which basic types are available in Go?
bool
string
// The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems
// and 64 bits wide on 64-bit systems.
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
When you need an integer value you should use ==int
== unless you have a
specific reason to use a sized or unsigned integer type.
If variables declared without an explicit initial value, which values they will
have?
Zero values:
0
- for numeric types,false
- for the boolean type, and""
- (the empty string) for strings.
How to convert values between different types?
The expression T(v)
converts the value v
to the type T
.
Is non-explicit conversion between numeric types in assignments allowed?
Unlike in C, in Go assignment between items of different type requires an
explicit conversion.
Which type i
, j
and k
will have?
When declaring a variable without specifying an explicit type (either by using
the :=
syntax or var = expression
syntax), the variable’s type is inferred
from the value on the right-hand side.
When the right-hand side contains an untyped numeric constant, the new variable
may be a which type?
It can be int
, float64
, complex128
, etc., depending on the precision of the
constant: