Go Day2

Boolean type

In Go a boolean is a type on its own and not an alias made from another type like an integer 0 or -1


Numeric types

Integers

Signed int type has varying size, but min 32 bits 8 bit (int8) through 64 bit (int64)

Unsigned integers 8 bit(byte and uint8) through 32 bit(uint32)

Arithmetic Addition, subtraction, multiplication, divider, reminder

Bitwise operations And, or, xor, and not

Zero value is 0

Can’t mix types in the same family (uint16+uint32=error)

Floating point

Zero value is 0

32 and 64 bit versions

Literal styles

  • Decimal (3.14)
  • Exponential (13e18 or 2E10)
  • Mixed (13.7e12)

Arithmetic operations

Addition, subtraction, multiplication and division

Complex numbers

Zero value is (0+0i)

64 and 128 bit versions

Built-in functions

complex – make complex number from two floats

real – get real part as float imag – get imaginary part as float

Arithmetic operations Addition, substraction, multiplication and division


Text types

Strings

UTF-8 Immutable Can be concatenated with plus operator Can be converted to []byte
Rune

UTF-32 Alias for int32 Special methods normally required to process e.g. strings.Reader#ReadRune


Constants


Naming convention

Typed constants

Untyped constants

Enumerated constants

Enumeration expressions


Constants can be any primitive type (int, boolean, float, string), arrays can not be constants because they are always variable

Can be shadowed

Arrays

Collection of items with same type

Fixed size

Declaration styles

a := [3]int{1, 2, 3}

a := […]int{1, 2, 3}

var a [3]int


Access via zero-based index

a := [3]int {1, 3, 5} // a[1] == 3

len function returns size of array

Copies refer to different underlying data

Slices

Backed by array

Creation styles

Slice existing array or slice

Literal style

Via make function

a := make([]int, 10) // create slice with capacity and length == 10

a := make([]int, 10, 100) // slice with length == 10 and capacity == 100

len function returns length of slice

cap function returns length of underlying array

append functions to add elements to slice

May cause expensive copy operation if underlying array is too small

Copies refer to same underlying array

Leave a Reply

Your email address will not be published. Required fields are marked *