Anda di halaman 1dari 13

Go Pointer

Maleakhi Rifandy

maleakhi@salt.co.id
Pengertian Pointer
Variable yang pada fungsinya menyimpan alamat memory dari suatu data / variable lainnya.

Biasanya disimpan dalam memory RAM (Random Access Memory), dan biasanya dapat dikatakan mereka
saling terhubung

Jika kita merubah nilai pada salah satunya, maka nilai pada alamat yang berhubungan juga akan berubah.

Format Penulisan Tipe Datanya (*) : Format Penulisan referensasikan alamat using (&):

*akan menampilkan output alamat


memory yang dipakai variable
(variabelSample)
Pengertian Pointer

● Variable b memiliki nilai yaitu 10 dan alamat memori 0x00010c008


● Maka variable a dapat menyimpan alamat memori dari b
● Artinya alamat memori b sebagai rujukan variable a, sehingga alamat memori variable a
adalah 0x00010c008, dengan syarat variable a bertipe pointer.
Pengertian Pointer
● Address of Operator [&]
- Mendapatkan suatu alamat dari sebuah variable

● Dereference Operator [*]


- Mengambil nilai asli dari sebuah alamat memory
● Variable a memiliki tipe data value int
dan mempunyai nilai 10
● Variable b memiliki tipe data value int
dengan tipe *pointer mengacu pada
nilai variable a
● Alamat Memory b berasal dari
variable a dimana memiliki nilai 10
Pengertian Pointer
Manfaat menggunakan Pointer :
● Membuat Variable menjadi Dinamis
- Tidak perlu membuat variable baru, meminimalisir penggunaan memory

● Send Variable as parameter function


- Mengolah Nilai Variable diluar func atau pada function

*variable myScore berubah menjadi 30,


setelah pointerParamFunc di eksekusi

● Upsizing Performance – Memory Handling


Best Case When Using Pointer

● Menggunakan Variable yang isinya besar (Static)


● Membedakan nilai nil / tidak nil
● Resource Connection (RDBMS Connection) / Connection
to other
● Butuh Mengubah Nilai Struct (Method Receiver)
Method Receiver
Method is just a function with a Special Receiver type between the func keyword and the method name.
The receiver can either be a struct type or non-struct type.

The above snippet creates a method named methodName with receiver type typeStruct. 


ts is called as the receiver and it can be accessed within the method.
Func vs Method
Function is declared by specifying the types of the arguments, the return values,
and the function body.

Method is just a function with a receiver argument. It is declared with the same
syntax with the addition of the receiver
Value Receiver vs Pointer Receiver
Value Receiver : makes a copy of the type and pass it to the function.
• Holds an equal object but at a different location on memory
• Any changes done on the passed object will remain local to the
method
• The original object will remain unchanged

Pointer Receiver : passes the address of a type to the function.


• The function stack has a reference to the original object
• Any modifications on the passed object will modify the original
object.
Q&A

SALTacademy.id
THANK YOU

SALTacademy.id

Anda mungkin juga menyukai