Pointers - Go Programming Tutorial #6

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

This video tutorial covers the basics of pointers in Go programming language, providing a foundational understanding of memory management in Go.

Full Transcript

[Music] what is going on guys welcome back the go programming tutorial series in today's video we're going to talk about pointers so let's get right into it all right so we're going to talk about pointers in the go language and pointers are basically just objects or entities that point to the memory address of other objects or entities so pointers in go are not as essential and important to the language as that is the case for a language like c or c bus for example where we need pointers for the most basic things all the time in go we don't need pointers for everything but we can use them and they allow us to do things we could not do without pointers so let's look at a quick example let's say i have a variable a here which is an integer and i say a equals 10 for example now what i can do obviously is i can go ahead and say fmt print line a and of course i'm going to get 10 i'm just printing the value of a simple variable nothing too special about it uh now let's say i don't want to know what a is or what the value of a is i want to know where a is stored in the memory i want to get the memory address if i want to get that i have to use the and operator here so this is just the basic and operator the referencing operator this is going to give me the memory address so if i run this you're going to see that i get something that looks like a memory address it's a hexadecimal number this is the memory address of this particular variable so if i now store this address into a variable this is going to be called a pointer so if i say for example ptr for pointer is and a this is now the same thing but i'm storing it in a variable so i can go ahead here and i can pass ptr and i'm going to get the same result not necessarily the same memory address or maybe even the same memory address no not the same memory address because it's just different this time that doesn't have to do anything with the variable here this is just because the memory address changes but as you can see i can do the same thing and i can also go ahead and de-reference that pointer because this pointer is now pointing to the memory address of a if i want to have the value using that pointer i can just dereference that pointer using that operator here and i can get the value of a or the value of the thing that this pointer is pointing to so if i run this you're going to see that i get 10. all right so why is this important and why is this useful this is useful because of a call by reference i can do a call by reference using a reference or a pointer which is very different from a call by value so let's go ahead and say i have two functions one function is going to be called change and it's going to accept a value so i'm going to call it change value it's not going to actually i mean it is going to change the value but uh the name is chosen because it's going to change something and we're going to pass a value to it so we're going to say change value and it's going to get a value which is going to be an integer and it's going to do something with that integer so then i also have a second function which is going to be called change reference it is not going to change the references it is going to change the value but it's going to get a reference as a parameter or a pointer asset parameter so we're going to call this change reference and here we're going to not get just a value we're going to get uh an integer pointer this is how you specify that as a parameter type we say this is going to be an integer pointer pointer to an integer and we're going to do something here alright so if i now go ahead here i can in both functions i can go ahead and just print the value so if i do print line and then i say value and print line value that is not going to make a difference i'm going to be able i mean maybe i'm going to have to re dereference that but besides that i can do the reading operation without any problems so if i call change value and i have a variable here let's say again var a int a equals 10 and i pass a here change value change reference i have to pass the reference to a there you go so we get the memory address here but i can de-reference that and you can see that i can do both things i can do the same thing with both functions so that's not a problem reading is actually the same i can print 10 and i can print 10. now what happens if i actually want to change something what happens if i actually want to change the value of this past value so if i'm not passing a constant i'm passing a variable so here i'm passing a which is 10 and here i'm passing a reference to a so if i change the value of a what's going to happen let's see if i say value equals 200 here for example is this going to work or is this not going to work we don't get any compiler errors we just don't know yet and i can do the same thing here and i can say value equals 200 or actually so that we can distinguish it i'm going to say equals 500 and i'm going to only call one of those so i'm going to call change value we started with a variable a uh what do we have here now cannot be represented by type oh yeah of course we need to use the star here there you go dereferencing so what we do here is we have a variable a which is an integer we set this value to 10 and then we pass that value as a value not as a reference to the change value function which sets that value to 200. now if we change the value and i then try to print a is this going to be changed or not i need to import fmt there you go and if i run this right now you're going to see that a is not going to be changed it was 10 before that and it's going to be 10 after that i can do this again one time before that one time after that you're going to see even though i change something to 200 here it is going to remain 10. now if i print the value in here so if i say fmt.printline value inside of that function here you're going to see that it actually is 200 so this value changes but a does not change because this value changes only in the scope of that function because what we do here is we're not passing a as an object we're passing the value of a we're copying the value we're saying okay a is 10 and yes i'm passing a but i'm not passing the actual variable a i'm not passing the object a into the function i am passing the value of a and you can deal with that value whatever you like but a is still a if i want to actually do something with a itself i need to pass the reference to a this is what i do with the change reference so if i do change reference here and i pass not a but a reference to a you can see let me just remove that print statement here you will be able to see that a enters the function with the value 10 and leaves it with the value 500 why because we're not just copying a we're not passing the value of a and that's it we're passing the actual memory address of a we're passing the location of this specific object which is going to be then dereferenced inside of the function and we're going to set the value if not the parameter we're going to set the value of the actual value of the actual object in that memory location and this is why we can change it when we do a call by reference and we cannot change it when we do a call by value now i also want to show you one more thing which is how you can define pointers using the bar keyword if you want to define a pointer and not set it yet you just say var ptr and then you use the dereferencing operator so star int and that's your pointer then if you have something like a equals 10 you can then go ahead and say ptr equals and a like that and you can do whatever you want with that uh you can just go ahead and say fmt dot print line ptr this is not the right module there you go so we can do that and also one thing i want to show you now i don't know if you're going to need this in this tutorial series but we can also have a pointer that points to a pointer because the pointer itself is just a variable so the pointer itself is also an object in the memory so we can point to a pointer and in order to do that we can just go ahead and create a pointer pointer which is going to be var ptr pointer pointer and this is going to just be two stars here so pointer to an integer pointer and if i want to set this i'm just going to say pptr equals and ptr and i can go ahead and print this as well this is just going to be the value of uh this is just going to be the memory address of the first pointer so this is also something that you can do and i think we should also be able to dereference it first of all if i dereference it one time i'm going to get the pointer to a and if i dereference it a second time i'm going to get the value of a so if i add two stars here i'm going to get the value of a now i'm not sure if we can do this as long as we want so why not actually so if i add a lot of more stars this would also work but then of course you would have to have enough pointers so this is just something that i want to mention that you can do if you need it somewhere you can do pointers to pointers to pointers to pointers you can also use different data types you can point to point to point to point to string and so on uh so this is also one thing that you can do in go so that's it for today's video hope you enjoyed hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you

Original Description

In this episode we learn about pointers in Go. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the basics of pointers in Go, including declaring variables, understanding memory management, and using the address of operator. Viewers will gain a foundational understanding of pointers in Go and be able to apply this knowledge to build more efficient programs. By the end of this tutorial, viewers will be able to declare variables, use pointers, and optimize memory usage in their Go programs.

Key Takeaways
  1. Declare variables in Go
  2. Understand memory management basics
  3. Use the address of operator to get the memory address of a variable
  4. Pass variables by reference using pointers
  5. Optimize memory usage in Go programs
💡 Pointers in Go are used to store the memory address of a variable, allowing for more efficient memory management and data manipulation.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →