This tutorial provides a comprehensive introduction to Go (Golang), covering essential concepts including package structure, variable declaration with static typing, array and slice operations, map usage, conditional statements, for loops with range iteration, function definitions with multiple return values, error handling using the errors package, custom type creation with structs, and pointer manipulation for pass-by-reference behavior.
Go Programming Crash Course: Core Concepts for Beginners
Added:you're doing don't back here give another video and today we gonna learn go we're gonna learn go fast a small disclaimer this is meant for someone who has some basic knowledge of programming from a sales language or something so I'm going to be using a basic programming terminology if you have no idea what the hell I'm talking about at any point you should probably go look up another video that tells you the basic is programming things like variables loops arrays stuff like that the first thing you need to do is install go if you want to use it on your system you get a good link don't work here there's a big-ass blue button here you click it to download the Installer or archive that you need if you get stuck you can go to the documents and go to getting started and it will kind of walk you through I'm instead of also installing it if you wanted to you can click this little link here called open and playground it'll basically give you like a web-based text editor that you can use to just mess around with go for a little bit actually you could this would actually be perfectly fine for everything that we'll be doing in this little video so with that let me go back over to my terminal I'm gonna create a file doesn't matter what you call it I'm just gonna call it touch because authorial who who else work my I have a like a tool thing in my editor that's already kind of like put in like a some boilerplate here and this is like the actual very basic structure for creating a executable go program and let's go over everything here so the first line any go file is the name of the package it belongs to in the case of the entry point of the go program it has to be called main otherwise you can kind of call everybody how you want keep in mind that within the same level of directory every file has to be in the same package if you try to if you have like a direct like if within your folder or directory we have three files and let's say like one file to or say they were in package a and then the third file assistant package be it won't be able to build it'll gonna bail he'll yell at you this is how you import excuse me other packages into your file to use this is from the standard girl library if you're going to be importing multiple things instead of having like 20,000 import statements today what you can do is you can have import and then open and close parentheses and then the name of your imports each new import would just be a new line like uh errors it'd be like that that's all that's that's all you would do it got rid of that automatically cuz I my plugins like automatically running stuff till I clean up my file and I save and then the entry point is a function and functions are defined with the func keyword followed by the name of the function in this case the entry point is called main it has no arguments and hasn't it return type and then just it just this case it just prints that message we're going to place that with the classic hello world that's not whether they're good and we're gonna run this and go hello world okay hello moto so you this is the night now you can very least like run go program right so this first thing you want to do but you probably want to store data and some variables okay so go has two syntaxes for messing around variables the first one use the VAR keyword the name of the variables we revive word and then the type of the variable go is a strong statically typed language so you have to basically every tie every every variable has a type of sign to it and then depending on what the type is in the case of a string you can't say assign an integer to this later because if you do and you try to run it it's going to come right at you after you've declared your variable you could say like a word equals and then we're gonna do hello world and instead of just being in here you can say word and if we run this there should be good so you can also initialize this with a value instead of making it later so like hello world this should still do the same thing there we go if you actually don't initialize this now in most languages that you probably use this probably throw an error saying well this is uninitialized it's oh it's something like this so it goes pretty much every type that I know of off top of my head has no concept of like no when you try to reference a variable that has not been initialized it's kind of implicitly initialized to what's called a zero value every type has a zero value in the case of strings it's basically just an empty string if it's an integer at zero if it's a boolean that's false if it's an array it's just an empty array things like that so actually if we actually run this if I saved it yeah who knows it's just empty a quicker syntax to work with and because we're all good programmers just made it we're lazy we like to type less as if we get rid of this you can do the name of the variable : equal sign and then whatever the value is now you'll notice that I didn't actually say what type it is when you do it this way go will actually infer what the type is from the value assigned to it so and just to verify that it actually is inferring correctly if I didn't later try to say well this is a I've and then I run this it's going to cry at me saying that I'm trying to assign it an ace integer to a string so that's the that's the very bare minimum that you need to mess with in terms of with variables let's say that you you want to store a cos of values though so you want to use like an array or something right so let's get rid of all this stuff let's say so we want a collection and it'll be X will do the who do this one of our collection equals square brackets and then inside the square brackets you want the length of the array so 10 actually coffee above and then the types of the values stored in the array so let's say hints they're all integers and then let's say I want to set the value of the value in index 1 to 5 and then we're going to print out the array run this you see zero five zero zero like pretty much every other program I've ever used arrays are zero indexed so the first value actually has an index of zero not one you can also use the same shorthand syntax with this so a bit it will be collection equals zero five and obsessed memory or a five inch and then curly braces and then you can define all your values here so I'm just like 1 1 2 3 4 5 never get rid of this and I'm gonna run this and 1 5 3 4 5 because I change the value Oh so go has a much easier way of working with arrays so if you exit go here and I get rid of this this is what's called slice a slicer basically goes out basically call them like smart replace their it like an abstraction over the standard arrays and they also allow you to do some extra functionality that standard arrays don't use so if I exit run this there should be the same thing one of the things that you can do is that it will dynamically grow and shrink as you as it needs to how do I come I believe if you're from Java there'd be like Oh was I call a dynamic array or something like that that is kind of like grows and shrinks its needed per I believe somewhere and goes documentation it does basically say that unless you have a very specific reason for wanting to use specifically in array 99.9% of the time you should just be using slices one of the things you can do with slices is you can use a built in function and go call it a pinned so if we want a collection to be something else will cause the collection equals append and then the original thing and I'm going to pinned on to the number 10 and if we go over here you'll see that it appended it correctly note a pen that does not mutate the original array it gives it does not mutate the original slice it gives you back a cop and new copy of the slice so what else do we got here so if you're from using other languages instead of arrays and those things something else you might commonly use is something that stores things and key value pairs in PHP this would be like an associative array and Python just be a dictionary and Java this be like a hash map something like that so the way you do this is I'm gonna have collection equals and I'll do leave this make and then you have what is this it is app and then here you have the the type the type of the keys sort of say a string and then the type of the values the keys are pointing to so whereas a int and I think this has a zero value too yeah so it's an empty map and then you can't assign values to here using kind of syntax that looks similar in PHP so ever have collection hello equals equals one we have collection world equals two we save that and run that you will see hello one world to write plus we got here you can also remove a entry from here by using another thing called delete so first you pass in the extra map excuse me and then you pass in the the key so dude we're gonna get rid of hello so you run this and it's gone it was never here to start with you can also if I recall there's a a a a kind of a shorthand syntax for initializing and map with values and I don't think you use I don't recall you make instead of that you do like collection equals map string oops oh it's like ants and then open curly braces I usually can't do this on new lines so it'd be like a hello one and then world then we're gonna get rid of these and I'm run this there do variables unless ya look scared of this gasps that works so here's a little quirk that's interesting was go if you were like defining initial values or something in multiple lines like this it actually makes you leave a trailing comma at the end of this or if you were to put like Newt and the more values afterwards see if I actually do this and I've Ernest it's actually gonna complain unexpected newline comma or Fela braces so it actually makes you do that whenever you're like defying self at multiple lines which is interesting it's like a little quirk that that go does okay so you can store single values a list of values and it excuse me and a map of values so best we want to do we want to how about you want to be able to do conditionals you want if statements to be able to like have your program do different things depending on conditions so just have standard if syntax unlike most languages that usually wrap the actual like condition in parentheses you don't do that and go so I'm going to say like what if collection at hello oops yeah is greater than three which it's not I'm gonna fmt print line greater than three then you also have else--if collection hello is let's say greater than do that's not yeah phrase into FM t print line later than two and then you also have else which will just see else and I'll just save everybody Panetta guy FM t print line plus what else so if we run this right now it should give you a else statement I'll ship and if a let's say I go up here and I changes this to for this should give you the first one greater than three if I change this to three run this right on - yeah they're good plus we got next loops so you want to you want to keep doing you when you want to like it it iterate over something a lot so let's get let's keep me let's get rid of all this junk so goes a little unique in that it only has one type of loop which is four loops you're probably like what that is making sense it'll make sense in a second so the syntax for it is kind of standard for most or languages so we're going to have I equals zero while high is less than 5 I plus plus I'm just gonna print it out right I'm just gonna print that out my suck ok 0 to everyone to the abhor but you can basically make these for loops kind of behave more like other types of loops in other languages so if you want this to basically be a while loop you would get rid of all this junk and this and this and I'm gonna move this out here and I'm gonna do I plus plus so basically this is basically saying while I is less than five to keep iterating and then here I'm just increasing I inside the loop itself so this should still do the same thing yep unless we got here um so something else you can do and with these four loops is you can actually iterate over a range of that like a collection of values automatically so if we have our let's say we have a array our slice so we have a slice of int now we have 0 1 2 3 4 5 right and you can say let's get rid of this we're gonna say 4 and it will return 2 things here that would turn the index and then the value I guess right so 4 equals range over X I want to print I see index equals index and then value because value this when you pass in multiple values like this to like printer print line each one of these will have a space separated I believe so if we run this you'll see you index value something you can do when you have something that's giving you multiple values like this and this also works with something I'll show you later is let's say we we don't care about the index this is rather common I've done this quite a bit let's say we don't care about the index something you could do so if you leave this alone and I don't actually use the the index I just I just use the value if I do this it was going to plant you because you declared of that a variable but didn't use it you can actually tell go to ignore a value by replacing the name of this variable with an underscore this is basically telling go I don't want to third away now if you do this that's fine you can also do this this is the exact same thing with they go back a little more instead of just being a ray of slice you can do this also with maps if I say this is the map was string and Ince and I say one two two and then so it says this being an index it'll be the key word let's step it to change word thank you okay this you'll see key one value one the same thing and again if you don't want the key or the value or something you can just put it underscore here to basically tell go to throw the away cause you don't want unless we got here one other thing was the for loop so there is basically what you want to call a a infinite or loop which is basically just like for so when you do this you have to explicitly tell go to break out of the loop otherwise it'll just loop forever so I way you would use this so let's say I have I equals 0 if FN FMT plants line I I plus plus and then if I is greater than 5 what is break so what this is do is that when I is your you're killing go to explicitly the break out of the for loop otherwise this will loop forever okay so that's that's known either that's basically like your infinite loop you can do until you explicitly tell it to break out of it let's see let's get out of here well Nexus okay I can we are did variables conditionals arrays and slices maps loops so let's so let's make some new functions about that so we've always but we've been in main hold on let's make some new functions so let's again if you see up above to define a function use the keyword funk and then the name of the function so let's do this is basic math some yeah blues some and then you pat then you say what arguments there are so in this case we're we're adding numbers so let's let's say we actually want some more arrests say what X is an int and Y is an int and then here you specify the return type of the function so though say returns an ends it just does it just returns X plus y that's that's all it does and then here we're going to say fmt dot print line sum of five plus five and we run this ten yay so small time-saver tip if you have a function definition like this and you have multiple arguments in a row that have the same type instead of explicitly saying what they are each time you can actually do this wait yes so what there's a go would infer that these are both integers like this that runs and it you could do this with as many as you want so let's say we said this was like it had three arguments and did plus the I did plus one see tourism and you'll notice I'm only telling it once what these types are because it's basically inferring that oh all three of these must be integers so say excuse me let's get rid of this function here so we're gonna do something else so functions if you if you can't guard from whenever we had the little deranged keyword functions can actually return more than one value at the blunts which is pretty interesting so let's say what's a so a square root is you take square roots in math a the square root of a negative number is like a complex number and it's not really something we want to mess with right so let's say we're going to find a function excuse me call square root and it takes takes a float yeah like flute can't spell words that a flute 64 and returns and if they have more than one you wrap it in parenthesis like this it's gonna return to float 64 and let's return to air see this is this is a very common thing to do and go where if a function basically has the Cape the possibility of airing you return the the main thing you want to return and then an error so we're going to do what if X is less than zero we're going to return zero because you know we're not doing anything and then so just two common ways that you can create in return errors excuse me one is to use the errors package and it has a new function and then you put in the error message so as I say X cannot be less than zero else we're going to return to square root which believe goes matt has a math package and it should have a square root function so I'm just going to use that and even though we're not returning an error we still have to take Lee return something here so if you're not actually returning an error you just return nil which nil and goes the equivalent of like null and PHP and JavaScript or none in Python says we're go let's keep the root go here I'm gonna say I is equal to 5 and I'm going to fmt dot print line square root of five I said that's part gonna complain that make it that's not very probably yeah eyes out genius yeah I need to do there see now it's a float okay and you see it says dil for the other part yes something I didn't do here which I could do I have to do because I'm being a big dummy is this return to values right ahead I and error is square root of my way I'm doing this dump equals five zero equals square I gonna call this X prax I'm going all over the place man I swear it so if you're if you're returning an error if I recall correctly you have to handle it if you don't check this I believe that go boy it won't actually compile undefined G yeah G let's see for the yeah error declared but not used actually so instead of the same not used I might even throw stuff away let's throw it away I think it might return a warning okay well if you're getting returned an error and you don't handle it I believe that runtime just panics if it actually gets an error so to handle this properly what you do is you're going to do if error does not equal by the way you're going to do this so damn much this whole if air is not no thing you'll probably write your own convenient way to basically just slap this in somewhere because you're going to do it a lot we're gonna do what FM t dot print line there was an error so right now there won't be an error but if I make this negative five now it's going to go there was an error okay so another way you can do this is instead of doing errors knew the FMT package that we're already using actually has a function that returns a formatted error message so to do that instead of using errors new year we're going to do FM t dot error F and we're going to do the value of x % F excuse me cannot be less than 0 and then we want to say pass in X all right so I'm gonna run this again unexpected cannot be I make a doodoo oh yeah you big penis I stuck more parentheses and I stuck more quotation marks in here yeah but right now there was an error I gotta go so instead of actually saying like just saying there was an error I could just actually print out to the error message so I stayed using print long I'm gonna use printf which is a similar to error F down there but it's just returning a string it's not actually like repairing an error I'm gonna say there was an error and then print out the error which would be error let's actually put this on over I want this on a new line run this you'll see there is an error the value of X negative 5 cannot be less than 0 and so that's it that's how you rate new functions return more than one type return errors handle errors things like that one other interesting thing you can do and I might goose this up a few times because I don't remember how you do this exactly off top of my head is so instead of just having return types here you can actually define the name of variables here so what let's just say this is um we're going to call this the route I was called the route every report this one error and what this does is whenever this function is called it will implicitly create these variables okay and then what you have to do is instead of doing um excuse me return here you just set these two values so we're gonna say like okay of course like any other variable these are initialized to their 0 values which in the case of a float is 0 in the case of an error is no so in this case we would do error equals fmt this basically this set to this so what is it uh this yeah thank this and I'm going to paste this your get rid of this I'll get rid of this cap and I'm gonna go else see see root equals math dot square root of x that I think that works I don't think you have to return and those okay your turn so this is like a different way of doing it it's a little weird you don't typically see it used when you're turning errors you typically see this used when you're returning like multiple actual values so like if this function was returning like say two floats you you might see this used an example of this being used like remember off the top of my head if you go to the the time package in the standard library there I believe there are a couple places in there where it makes use of this like predefined variable name syntax and the return types of the function so this should still work yoga that's just is a different way you can do it so we did functions we did air so this won't get rid of all this gobbledygook here so as goat is strongly tied you can define your own types a type you are going to very frequently probably do is called a struct struct is basically just like a um has a predefined shape of the multiple pieces of data think of it like a class in an object or head language like Java but it doesn't have the behavior attached to it I'm gonna have to type user struct curly braces and then here you have the the name of the property and then the type of the property so ver si excuse me name is a string and then ID is an int yeah as I sewed down here you would do like my user equals user and there's two different ways you can initialize them like this if you put it on a single line you could just do like a mob of five like this I'm gonna do fmt I don't know if I can actually do this like this or my user it might contain to me I did okay so you see this has these two values in this truck all the five if you are going to initialize these values on multiple lines you have to specify the the property name or so be like name is Bob and then ID is five again you have to have the trailing comma or a little screen with you if you want to access only a specific property on this struct you do it with the dot notation so like that name this will only give you the name if you want to change one of the properties on here you'd do the same thing you would say like my username equals no less we want to do so you you can do other things with types you can actually have types that are based on other like other types that are already in the system and I'm going to show you why you might want to do that in a second so you have say you want to type and I'm maybe a user has an email and you want to be a string so we want these these these email strings that have some specific behavior to them I'm going to show you how we do that so I'm going to say type of email is a strength which this part looks a little weird at first and instead of this thing this is a string we're gonna say this is a email yeah so what does this matter so it was I saved this email is Bob and Bob calm yeah that's a that's a lovely email name so never had we would making functions there's an additional part of a function declaration syntax you can do that is a little a little and unique to go so you can actually do what what it would amount to an object-oriented ish type of approach where you're attaching behavior to your types and you do this through what's called a receiver so you define your function like normal like this so we're going to say the name of the function is called we're just call it gonna call it validate it's not about taking arguments I'll show you why and it's just return a boolean so how do we tell that go that this function belongs to the email type we get before the name you do this we have parentheses like this and we're you you name the the thing it's attached to so I'm gonna call it a fee for email and use a say it belongs to the type email now within this function be the thing that's attached to that you're calling it from will be I like available so like I I don't I don't want to pull the regex off top of my head but let's just assume that the email is always right and it's gonna return true so you've attached behavior to this this type you've created so let's go down here and we'll say like if my user died email validate which I believe that should this be that account is true because it evaluates to true or false do fmt dot print line this is a valid email what do sometimes I get butterfingers on bins or else else fmt print line this is not a valid email I don't ask you they might get rid of this stuff run this and you will see this is a valid email it called a behavior attached to the type we created now what happened and again you would have liked that more proper logic in here like you'd use like red X or something to check whether this is actually like a valid email string or not we'll say it's false right now and we'll do overrun that's no say this is not a valid email so that's how you would attach behavior like like behavior to this now you don't usually see it done to things like this you usually see it attached to strux because essentially attaching functions to structs is the closest thing you sense they get to like object-oriented programming and go go has no concept of classes like you do see and like Java or something like that it kind of like dissuade you from being like super object-oriented with it so you can kind of force it to do that but it's a it's a little a little clunky some of us we got here tanks so instructs you can kind of define this like metadata type stuff called tags and you do that by having was I called backticks and one of them what these are for it depends entirely on your projects some third-party dependencies will use them to do certain functionality the most common when you're going to see is you're gonna see this thing called I think it's JSON and I think as it works it goes JSON didn't like and like I said it works I might have build but it's not export okay yeah okay since it brought this up I'll make mention of this and go like let's say this was it this is a package like a separate package that it was it was not an executable and you wanted to make like this structure available for someone else to use in their code the way code does this is you don't explicitly tell it to it like to export it like you say do in JavaScript would like to export keyword though the way go infers whether something is supposed to be exported or not or like made public is whether the they the first letter of the name is capitalized so if this is now exported excuse me if the name of your like function your type whatever if if the first letter of it is capitalized it means its public it'll be publicly accessible outside of this this this package but if it's if it's lowercase as it's pointed out here this is not export and it's not public and you'll notice it's kind of referencing a specific error and what that's referencing is it wouldn't matter what the JSON tag is it's not exported but you're firing one minute what what what does this JSON tag actually do so what this does is there is a JSON package ingo that allows you to encode and decode JSON to and from go Struck's like this by default it will just try to match up the the JSON fields with the names of with the names of the properties on the struct but if you want it to be like a specific syntax or has a completely different name you can define that in here home that's my own posture is going crazier please stop so like instead of this you could say like well in my JSON file it's not called name it's called first first name so what that'll do is if you try to decode a JSON message into this this particular shape of a this particular struct if it has a field and it called first name it'll put that value in this struct that's basically what that's basically what tags are for they basically add some like metadata that other packages can use to do certain behavior with it see what else we got here we have we've done variables conditions area array slices maps loops functions errors creating your own types something else that I personally have done with these a type aliasing thing like this is um let's say we have we we frequently do something like this where we have let's say like a map of the users like I put in so it actually not cry at me so say like users equals map int of user like that something like this right so let's say you do you do this a lot and sometimes you just don't want to write this out all the time and maybe you also want the the type the type name to kind of be more I guess semantic it was actually meant to be that what it's actually like holding so something I see frequently and some other projects is you can just create type aliases is kind of like shortcuts to things like these very specific like map type definitions so I could have like type I'm a call like user user map and it's a map of type with keys event and they hold users you basically do that so that instead of me saying that this is a map like this like I say is a user map and do that think I'm wearing out stuff here so I guess last thing I'll do is pointers so and go you can actually make use of pointers they made Gary of all this here I'm not really using it now we're down here thank you so let's say we have a number here we'll just call it x equals zero and we want to pass it into a function called sum again takes a x and y that are in it returns an int and all it's doing is turning yeah x + y all right and we want to excuse me see and it's we just do we just do this we just do some of X and then we went to fmt red line exit nice let's not do that let's do let's do we want to do like increase like Inc will be able do that increase and actually will say it doesn't even return anything what does do that and it was do X plus X plus plus yeah let's do this that makes way more sense is here it is so we're do increment X so what you may think happens is it okay increment X so this should be this should be one now right oops not comping you okay oops it's still zero why is that so this function if I can here like this this is what's called doing passing by value what this function is getting is a copy of the value in this variable to use within this within this what's called scope but once you leave this scope it that it was only like only usable within that particular function invocation that is gone didn't touch this at all if you're one thinks it's like - like mutate the value of this original variable you have to do is called pass by reference and do that you're basically passing in a memory reference to and to annotate that in the actual function definition what you're doing look at my castles are damn mine you put an asterisk in front to indicate that this is a pointer and then to pass in the actual memory address you put an excuse me a verse ampersand in front of this this basically gives you the the memory address of this variable not the value and in order to actually properly increment this we need to us call it dereference that with a asterisk in front if you don't do this and you just do this what you'll be increasing is the memory address not the not the not the value that the memory address is pointing to it's like so let's share an example if let's say I I print this but the word so you see it print out like it's a bunch of gobbledygook that's basically the memory address that variable when this program but if we if we do this now you will see that the value has indeed been mutated I'm not really gonna go a lot over pointers that's just a kind of like to show you how you would how you would use pointers fun fact if you have us whenever you're defining types you could actually say that they so you like you say you have a struct let's say we have out of type my struct struct instead of this being a so say odd counter is int instead of saying this is an int you can access a this is a pointer you can actually have the strux be holding pointers not the actual values there's a couple of reasons why you might want to do this sometimes maybe performance related stuff maybe you want to do very complicated things with this I don't mess around a lot of this with like very specific memory man and stuff so I don't but just a just food for thought that you can do that I believe at this point uh that should be all you should be able to really need at this point you should be able to write a very simple go program you can you know you can store data in vat variables you can store excuse me a collection of values and arrays or slices and maps you can do you know your standard conditionals their statements you can loop with for loop or the one the range operator you can create new functions you can return multiple values from those functions you can return errors and handle the errors you can throw away values from functions that return multiple values if you don't need them you can create your own types you can attach methods to those types to allow them to be used specifically within like that fat that that type and you can messy pass stuff around by excuse me by reference instead of value that's all I got for this video there are a couple other more advanced ago topics I'll talk about in later videos such as concurrency maybe setting up like a development environment the Pennsy management was interesting go to that's all I got for this video if you liked the video you're on YouTube sure drop me like and subscribe you own Facebook be sure to drop me uh I guess wanted me out like who like and follow but above all the best thing you can do this video is to share it so that it can help someone else out and with that y'all come on back now now see you next
Up Next

Go Lexer and Parser | Saving and Loading Picture Trees | EP 20
@JackMott
1.3K views•2018-02-16

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

FastAPI vs Flask vs Django: Choosing the Right Python Web Framework
@TechWithTim
302.5K views•2024-05-26

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies



































![Learn GoLang Basics with Hands-on Demos [ 2 HOURS ] | Sandip Das](https://i.ytimg.com/vi/WiIA0eU9RxI/sddefault.jpg)

![Go Programming Course [01]: Setup, First Program, go.mod, Build Tools & Makefile](https://i.ytimg.com/vi_webp/OzdE_PJoz4U/maxresdefault.webp)
