Go Language Expressions

Expressions
Mostly C-like operators.
Binary operators:

Prec

operators

comments

6

* / % << >> & &^

&^ is “bit clear”

5

+ – | ^

^ is “xor”

4

== != < <= > >=

3

<-

communication

2

&&

1

||

Operators that are also unary: & ! * + – ^ <-
Unary ^ is complement.

Go vs. C expressions
Surprises for the C programmer:

fewer precedence levels (should be easy)
^ instead of ~ (it’s [...]

Expressions

Mostly C-like operators.

Binary operators:

Prec

operators

comments

6

* / % << >> & &^

&^ is “bit clear”

5

+ – | ^

^ is “xor”

4

== != < <= > >=

3

<-

communication

2

&&

1

||

  • Operators that are also unary: & ! * + – ^ <-
  • Unary ^ is complement.

Go vs. C expressions

Surprises for the C programmer:

fewer precedence levels (should be easy)

^ instead of ~ (it’s binary “exclusive or” made unary)

++ and — are not expression operators

(x++ is a statement, not an expression;

*p++ is (*p)++ not *(p++))

&^ is new; handy in constant expressions

<< >> etc. require an unsigned shift count

Non-surprises:

assignment ops work as expected: += <<= &^= etc.

expressions generally look the same (indexing,

function call, etc.)

Examples

+x

23 + 3*x[i]

x <= f()

^a >> b

f() || g()

x == y + 1 && <-chan_ptr > 0

x &^ 7 // x with the low 3 bits cleared

fmt.Printf(”%5.2g\n”, 2*math.Sin(PI/8))

“hello” “, ” “there” // lexical cat

“hello, ” + str // dynamic cat

Numeric conversions

Converting a numeric value from one type to another is a conversion, with syntax like a function call:

uint8(int_var)       // truncate to size

int(float_var)        // truncate fraction

float64(int_var)    // convert to float

Also some conversions to string:

string(0×1234)     // == “\u1234″

string(array_of_bytes)   // bytes -> bytes

string(array_of_ints)      // ints -> Unicode/UTF-8

Constants

Numeric constants are “ideal numbers”: no size or sign, hence no l or u or ul endings.

Example:

077 // octal

0xFEEDBEEEEEEEEEEEEEEEEEEEEF // hexadecimal

1 << 100

There are integer and floating-point ideal numbers; syntax of literal determines type:

Example:

1.234e5

1e2 // floating-point

100 // integer

Constant Expressions

Floating point and integer constants can be combined at will, with the type of the resulting expression determined by the type of the constants.

The operations themselves also depend on the type.

2*3.14        // floating point: 6.28

3./2    // floating point: 1.5

3/2     // integer: 1

// high precision:

const Ln2

= 0.693147180559945309417232121458\

176568075500134360255254120680009

const Log2E

= 1/Ln2       // accurate reciprocal

Representation is “big enough” (1024 bits now).

Consequences of ideal numbers

The language permits the use of constants without explicit conversion if the value can be represented

var million int = 1e6      / float constant

math.Sin(1)

Constants must be representable in their type.

Example:

^0 is -1 which is not in range 0-255.

uint8(^0)     / bad: -1 can’t be represented

^uint8(0)     // OK

uint8(350)   // bad: 350 can’t be represented

uint8(35.0) // OK: 35

uint8(3.5)    // bad: 3.5 can’t be represented

No related posts.

Leave Your Response

* Name, Email, Comment are Required