Variables

var val = 10 // the val at memory 625 (lvalue) contains the value 10 (rvalue)
var number = 5
var ptrNumber = &number // the rvalue of ptrNumber is the lvalue of number

fmt.Printf("%d\n", ptrNumber)  // will print the lvalue of number: 2292 ()
fmt.Println(*ptrNumber)        // 5 (* dereference the address; gets the rvalue of the pointer address)

*ptrNumber = 10                // change the rvalue of number
fmt.Printf("%d\n", ptrNumber)  // 2292
fmt.Println(*ptrNumber)        // 10
fmt.Println(number)            // 10

the lvalues above are assigned by OS; the numbers used above are only examples. On 64 arhitecture the numbers will be bigger and

More examples

define: exists in the "compiler table" with lvalue is assigned

declare: exists in the "compiler table" with a lvalue not assigned

initialize: assign rvalue?

// variables are stored on the stack and are initialized to zero value of type: ""
var hello string
var helloWorld string = "Hello World"
var world = "World" // shorter 

// multiple variables
var foo, bar int // implicit initialization to 0
var one, two = 1, 2 // explicit initialization
var three, four int = 3, 4 // explicit initialization

// short variable declaration
ciao := "Ciao Mondo" // only inside functions

If a variable is not explicitly initialized, it is implicitly initialized to the zero value for its type, which is 0 for numeric types and the empty string "" for strings.

When creating a variable from slice, map, channel, interface, functions, the value created is called header. All header values contains a pointer to the underlying structures.

Variables are stored on the stack. Pointers, maps, slices, channels on the heap.

results matching ""

    No results matching ""