golang object oriented programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right now we're going to do oop i'm going to clear this out from the last video and we'll just do this regular okay so package i mean all right and uh okay how you declare is struct here right all you have to do is declare the type first then the name of the struct we'll call the struck record okay record we'll put type and then we put struct after there right you can also declare types like you can put string here to declare type you know and it'll it'll be just like it'll it'll come out of the string put it in 32 or whatever we can put you can put anything here right you can put you know any like primitive type or we're going to make an object with a struct okay and then the convention is so we'll just put in name and then we'll call this a string okay and no comma after here then we'll put id we'll also call this a string right all right so now when we funk main so we'll just declare a record so we'll say r and uh we'll call that the assignment operator in record and then we'll call name just kind of like jason we'll say bob and then [Music] then id one two three four five okay so we can let me import import fmt so we can print this out on the screen oh man see that's one thing about go and be if you don't use the package it won't you know it won't compile so visual studio code does a pretty good job of making sure you use everything it's it's in the go format package or whatever or go format command or whatever you want to call that all right so fmt dot print line all right and we'll put r in here right and that'll be that so clear go run and we'll call main.go pretty sure you can do a dot here too because i because i already set up because i've already done the gold mod in it yeah it'll automatically look for this this right here because i've already done a gold mod in it for the last video and what i did was go my just just in case you didn't watch the last video you should it was on code splitting and then oop video and that creates a module right [Music] all right so we have our record right here right now say we want to create a constructor right we'll just call this new okay we'll just make a regular function and new record okay and you know what we'll put in nm string for the name okay so we'll just need a name and what it'll do is we'll return a record okay perfect so when we return this record okay two curly braces put a name it'll be nm in here and then we could just put some sort of default id the default id you know this is just for practice so it's for one blah blah just i just you know typed whatever there right so now we have a constructor so we can just say new new record okay and then we'll put in there um john bobbitt okay so let's see what happens now we got john bobbitt you know that's the name and then we get the id so we have basically constructor here for the struct right now let's say we want to format this uh format this a little a little better right we don't want to just use this print so let's just uh so we'll put show okay and what we have to do uh here what we have to do is special here is we have to pay make a pointer to the record okay so our record okay so we can access everything we can access everything in the uh inside of the uh inside of the strut so what we'll do is a format uh print line and usually you could use print f but you can use whatever right so we'll put r dot id first in this case and this plus will concatenate a string so we'll just you know we just throw something here uh is the name is the id and then we'll put another plus here r dot name which is [Music] which is the name from the string and we'll put is his name is this person's name let's be all inclusive here all right so now instead what we'll do we'll print out the string now and we'll do it right on the object right so r dot show okay is the id okay let me just clean this up a little bit so you can see you kind of got it running all together okay so we have this is the id and john bobbitt because john bobbitt is his name okay and there we go all right and so if we want to put this in another package right we can create another package and we'll call this uh we'll call this record okay so new folder and we'll call this record okay and then we'll make a new file in here record dot go okay and then we'll put package record import fmt okay and now we can just take this code right here and put it in here all right and now we'll import that record package import record [Music] okay so now oh it'll it'll be uh oop video right here because remember this is the uh that is the outside package that's the package that i made right and we'll make sure that this is gonna be called record so record okay so we'll put record that record.new record okay perfect and it took away the fmt package right so now everything should work [Music] so you see it works right all right so just to recap now we put this in its own its own package right here right [Music] and just it just has the struct record and in order to make our uh this is our constructor and it returns a record with the name you choose and then a default id and then we have the show function or the show method that this is the magic right here right it takes a pointer to the record so whatever you pass in so whatever it is this is going to come from this is going to make the dot operator right so when we put the r dot show the r is what's going in here or what's going in here this is basically how you do this or operate inside of the values inside of the struct okay all right so we showed how to quote split the code into its own little package and uh you know we can also do this right we can also do this right here and pull it out okay now we'll just delete this we have to change this to package main and we can keep it in the same same directory right so instead we'll just get a new record like that and then i'm not sure if this work let me try this just go run dot yup it works works perfectly right so it's because it's in the same package what i did you know i've changed the package main so we can just we can split our code that way too right all right
Info
Channel: coderlyfe
Views: 141
Rating: undefined out of 5
Keywords: golang oop, object oriented programming golang, golang constructors, golang class methods, golang structs, golang classes
Id: Ar7hLZtjcZY
Channel Id: undefined
Length: 11min 10sec (670 seconds)
Published: Sun Oct 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.