Go (Golang) Tutorial #17 - Receiver Functions with Pointers

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
copilot browser tools make the frontend reviewable
Learn how GitHub Copilot browser tools make frontend reviewable, enhancing collaboration and code quality
Dev.to · Paulo Victor Leite Lima Gomes
📰
Why Your React App Freezes Even With Zero API Calls (And How Web Workers Fix It)
Learn how to prevent UI freezing in React apps even with no API calls, using Web Workers
Dev.to · ARAFAT AMAN ALIM
📰
React 19 Features — What Actually Changed and What I Use
Learn about the new features in React 19 and how to use them in your projects, with a focus on practical applications and real-world examples.
Dev.to · Safdar Ali
📰
The Share Button Is the Product: Engineering a Viral Loop in Vanilla JS
Learn to engineer a viral loop using a share button in Vanilla JS to increase user engagement and retention
Dev.to · yunjie
Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →