Last active
February 17, 2023 15:14
-
-
Save Yigaue/d0a0128a86914ef718ed48457d003031 to your computer and use it in GitHub Desktop.
See in action on playground https://play.golang.org/p/8zBXTbZR88f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
var child string // Declared(type must be specified) | |
/*---------------NOTE------------------------- | |
Define = Declaring + assigning of value to variable | |
e.g: var child string = "Alex". // type is optional. | |
---------------------------------------------*/ | |
func main() { | |
child = "Alex" | |
// Here we're assigning the value "Alex" to the already declared variable child. | |
var childPointer = &child | |
// childPointer is a pointer because it stores the memory address of another variable 'child'. | |
fmt.Printf("The value of variable child is %v\n", child) //Output: The value of variable child is Alex | |
fmt.Printf("The memory address of variable child: %p\n", childPointer) | |
// Output: The memory address of variable child: 0x54ddb0 | |
fmt.Println(&child) | |
// We can directly get the memory address by attaching "&" in front of the variable. | |
//Print the memory address of variable child: 0x54adb0 | |
fmt.Println(*&child) // returns the value in the memory address which is the same as | |
fmt.Println(*childPointer)// . If we need the value stored at a memory address we put deference("*") | |
//symbol in front of the pointer. | |
In simple terms, another word for a pointer is memory address of another variable. So a variable has address and holds a value. | |
//---------------------------------- | |
// |||||||| More Example ||||||||||| | |
//---------------------------------- | |
var i1 = 5 | |
fmt.Printf("An integer: %d, its location in memory: %p\n", i1, &i1) | |
var intP *int | |
// intP is a variable(pointer) that will stores memory location of value of type int. | |
//Here it is declared but not assigned. The "*" indicates that intP is a pointer variable and not and ordinary variable. | |
//This means intP will store a pointer of value of type int. Note the use of (*) symbol in front of the type | |
//is different from when its in front | |
// of a pointer variable e.g *intP as use in the print statement below. | |
// Two things, when the "*" is attached to a type and when it's attached to a variable. When attached to | |
// a type, it only indicates that the variable of that type is a pointer(i.e storing a memory address or location) | |
// The second case, which is when atttached to a variable, it just means that the variable is aleady a pointer and | |
// we're just getting the value of that variable or the value in that address. | |
// Picture a building that serve like a kind of variable, that has an address and contains things. | |
//We can create a variable that stores the address of this | |
// building or the content of the building. So if the variable holds the address of the building then it's a pointer, | |
// but if it holds the content of the building then it's a regurlar variable. Also if we know the address of the building we can | |
// also be able to get the content of the building. | |
intP = &i1 // Here we assigned the memory location of variable i1 to intP. That makes intP a pointer. | |
fmt.Printf("The value of memory location %p is %d\n", intP, *intP) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This note is on pointers in Go programming language.