Go (Golang) Tutorial #17 - Receiver Functions with Pointers
Skills:
Algorithm Basics60%
Key Takeaways
Creates receiver functions with pointers in Golang to update the original value
Full Transcript
all right then so we've seen how to create receiver functions to associate functions with a particular type like this i'm going to create two more now so i'm going to create one to update the tip right here and also one to add items to this items map right here now i'm going to take these out that we added in the last lesson so we start with an empty map and we're going to use a function to add items so let's create these functions down here so first of all want to update the tip so funk and then parentheses to take in the bill like this this associates it with built objects then the name of the function which i'm going to call update tip and this takes in a tip argument which will be of type float64 that's what we want to update the tip to and remember that matches the type right here it doesn't return anything so we don't need to specify that and all i'm going to do inside is take the bill say dot tip and set it equal to the new tip that we take in okay so that's to update the tip next i want to create a function to add an item to the bill all right so let's create a function to do this func and then again we take the bill to make it receive a function we'll call this add item and inside we take two arguments first of all the name of the item which is going to be the key inside the map a string and then secondly the price of the item which is going to be a float 64. remember for the map we said the keys must be strings and the values are float64 all right so again we don't return anything inside here all i want to do is add a new item to the map so we say b dot items which is the map name and to add a new key it square brackets the name of the key which is a string the name in our case that we pass in and we set it equal to the price simple okay so if we try this is it going to work well let's go over here and i'm just going to use the update tip method first of all so i'll take my bill and call update tip and i'm going to pass in 10 something like that now i need to add the tip into the formatted string that we create right here so over here there's no sign of the tip we just cycle through the items and add them up but i also want to output the tip as well so i'm going to add in a line right here before the total which is going to be the tip so we're doing the same kind of thing here as the total all we're doing is creating another formatted string which we're adding to fs this string right here and inside we have a label on the left which is tip with some spacing and on the right this value is b.tip so it accesses the tip from the bill all right then so is it gonna work we've added the tip right here are we gonna see it on the formatted bill well let's give this a whirl i'm gonna clear out the console right here to give us a bit of room and then run the files and we don't see a tip it's still zero dollars now you might already know why this has happened because we have talked about this briefly in past videos and it's because remember when we call a function when we're taking arguments into a function we said that it creates copies right but also on receiver functions i mentioned in the last video we also take a copy of the bill we don't actually take in the bill right here that we call it on we just copy this bill and pass that into the receiver function and all we're doing is updating the copy of the bill right here so we said that when we're creating a copy if we created a pointer and passed that into a function it creates a copy of the pointer but then inside the function we can update that to update the original value because it's still pointing to the original value right so what we could do is pass in a point here instead to say look we want a pointer to a bill and inside the function we can de-reference this so i could say this is how we dereference a struct parentheses like this around the struct and then the dereferencer okay so this dereferences it now and then we access the tip on the original value and update and this is going to work but when we're working with structs like this we don't actually need to de-reference it if we're taking a pointer then go is automatically going to dereference it for us so that's a nice shortcut we don't have to do anything else in here we just have to pass in the pointer or say we want to receive the pointer as the bill now we don't have to do anything special over here when we call it go is automatically when it looks at this gonna pass in the pointer to the bill and make a copy of that to pass in instead of the copy of the value itself does that make sense and since we're now passing in a copy of the pointer when we're updating it which is automatically being dereferenced then it's going to update that original value so a rule of thumb whenever we're calling a method where we're updating the value we're passing a pointer so i'm going to do that right here as well because we're actually changing the item right here okay updating it now also there's another reason you might want to pass pointers into functions or receiver functions and that is because then we're not making copies every time we call a function so if i was to call this function quite frequently then i'd be creating a new copy every time of the bill if i just passed in a pointer i'm only making a copy of the pointer now if the bill object was very large and very complex then to make a copy of that is more work than to make a copy of the pointer right so i might want to pass in a point here as well and i don't have to do anything different inside here everything's still going to work the same because remember for structs pointers are automatically dereferenced so we can still get the original value by doing nothing extra inside here and generally you will either see receiver functions all with pointers or without pointers so i'm going to keep them all as pointers and then hopefully this is now going to work if i run this again we should see the tip now be ten dollars so yeah we can see tip is ten dollars now we've not updated the total here to include the tip so let's do that right where we use the total i'll say total plus b dot tip and save that run it again to make sure this is worked and we can see now the total is ten dollars all right so let's try using this function as well over here in the main file so i'm going to say my bill dot add item and the item i'm going to add is onion soup and then a price i'll just say like 450 or something like that now i also want to call this a few more times but instead of me typing this out i'm going to copy it from the course files and we can see we have three more items veg pie toffee pudding and coffee so if we save this now hopefully we're gonna see all of these added to the formatted string and see them in the console and we do and that looks a lot better we're getting there so we have all of the items the tip and then the running total of all of these things at the bottom cool
Original Description
Hey gang, in this Golang tutorial you'll learn how to use receiver functions, using pointers to the type instance -- so we can update the original value.
🐱👤 View this course in full without ads on Net Ninja Pro:
https://netninja.dev/p/learn-go-golang
🐱💻 Course Files:
+ https://github.com/iamshaunjp/golang-tutorials
🐱👤 JOIN THE YOUTUBE NET NINJA GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 Useful playlists:
+ Modern JavaScript - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
🐱💻 Download Go - https://golang.org/dl/
🐱💻 Go Numeric Types (bits) - https://golang.org/ref/spec#Numeric_types
🐱💻 Go Standard Library (bits) - https://golang.org/pkg/
🐱💻 VS Code - https://code.visualstudio.com/
🐱💻 Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
copilot browser tools make the frontend reviewable
Dev.to · Paulo Victor Leite Lima Gomes
Why Your React App Freezes Even With Zero API Calls (And How Web Workers Fix It)
Dev.to · ARAFAT AMAN ALIM
React 19 Features — What Actually Changed and What I Use
Dev.to · Safdar Ali
The Share Button Is the Product: Engineering a Viral Loop in Vanilla JS
Dev.to · yunjie
🎓
Tutor Explanation
DeepCamp AI