Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Pointer example in Go

By default variables (non struct) are passed by value in Go, and if modified in the function they are not reflected in main function as the values are passed as copy. In order to modify the value in method variables need to be passed by reference, here two operator in go lang is used to pass and access the reference.

& - Address of operator
* - Value of operator

Address of operator passes the variables address to the method, so this makes the variable to passed as reference instead of value and in order to access the value from reference * operator is used inside the method.

func main() {
    var num = 10
    modifyNumberWithPointer(&num)
    fmt.Printf("%d\n", num)

    var valueVariable = 10
    modifyByValue(valueVariable)
    println(valueVariable)
}

func modifyNumberWithPointer(num *int) {
    *num = 20
}

func modifyByValue(num int) {
    num = 20
}