DEV Community

indiewebdev
indiewebdev

Posted on

Golang Notes - 2

Variables

var age int = 30

underscore _ blank identifier

short declaration := colon equals

age := 30

only works block scope
cannot define a var with short declaration in global scope

at least one new variable should be defined

Multiple Variable Declarations

car, cost := "Audi", 5000

var (
    salary    float64
    firstName string
)
Enter fullscreen mode Exit fullscreen mode

var a,b,c int

swapping variables

var a, b int
a, b = 4, 5
a, b = b, a
Enter fullscreen mode Exit fullscreen mode

Types and Zero Values

if value not provided explicitly
Go is statically (type checking at compile-time) and strongly typed (don’t allow implicit conversions between unrelated types)

numeric : 0
bool : false
string: "" empty string
pointer: nil

Top comments (0)