GO Programming Language Declarations

Declarations
Declarations are introduced by a keyword
var
const
type
func
and look reversed compared to C:
var i int
const PI = 22./7.
type Point struct { x, y int }
func sum(a, b int) int { return a + b }
Why are they reversed? Earlier example:
var p, q *int
Also functions read better and are consistent with other declarations.
Var
Variable declarations are introduced by var.
They [...]

Declarations
Declarations are introduced by a keyword
var
const
type
func

and look reversed compared to C:

var i int
const PI = 22./7.
type Point struct { x, y int }
func sum(a, b int) int { return a + b }

Why are they reversed? Earlier example:
var p, q *int
Also functions read better and are consistent with other declarations.

Var

Variable declarations are introduced by var.
They may have a type or an initialization expression; one or both must be present. Initializers must match variables (and types!).

var i int
var j = 365.245
var k int = 0
var l, m uint64 = 1, 2
var billion int64 = 1e9 // float constant!
var inter, floater, stringer = 1, 2.0, “hi”

Distributing var

Annoying to type var all the time. Group with parens! Need semicolons as separators.

var (
i int;
j = 356.245;
k int = 0;
l, m uint64 = 1, 2;
billion int64 = 1e9;
inter, floater, stringer = 1, 2.0, “hi”
)

Applies to const, type, var

The := “short declaration”

Within functions (only), declarations of the form

var v = value
can be shortened to
v := value

(Another reason for the name/type reversal.)
The type is that of the value (for ideal numbers, get
int or float, accordingly.)

a, b, c, d := 1, 2.0, “three”, FOUR

These are used a lot and are available in places such as for loop initializers.

Const

Constant declarations are introduced by const.
They must have a “constant expression”, evaluated at compile time, as initializer and may have an optional type specifier.

const Pi = 22./7.
const AccuratePi float64 = 355./113
const beef, two, parsnip = “meat”, 2, “veg”
const (
Monday, Tuesday, Wednesday = 1, 2, 3;
Thursday, Friday, Saturday = 4, 5, 6;
)

Iota

Constant declarations can use the counter iota, which starts at 0 in each const block and increments at each semicolon.

const (
Monday = iota; // 0
Tuesday = iota; // 1
)

Shorthand: Previous type and expressions repeat.
const (
loc0, bit0 uint32 = iota, 1<
loc1, bit1; // 1, 2
loc2, bit2; // 2, 4
)

Type

Type declarations are introduced by type.
We’ll learn more about types later but here are some examples:

type Point struct {
x, y, z float;
name string
}

type Operator func(a, b int) int
type ArrayOfIntPointers []*int
We’ll come back to functions a little later.

New

The new() operator allocates memory. Syntax is like a function call, with type as argument, similar to C++. Returns a pointer to the allocated object.

var p *Point = new(Point)
v := new(int) // v has type *int

Later we’ll see how to build arrays and such.
There is no delete or free; Go has garbage collection.

Assignment
Assignment is easy and familiar:
a = b
But multiple assignment works too:
x, y, z = f1(), f2(), f3()
a, b = b, a

No related posts.

Leave Your Response

* Name, Email, Comment are Required