Learn Go in 12 Minutes

Learn Go in 12 Minutes



Views:250529|Rating:4.89|View Time:13:34Minutes|Likes:6389|Dislikes:146
Learn Computer Science at

An introduction to Go covering the fundamentals to get you up and running in 12 minutes.

The code is available on GitHub

—————————————-
Support this channel at
—————————————-

00:11 Installing Go
00:20 Creating a Go project
00:54 Structure of a Go file
01:48 Compiling and executing Go
02:31 Variables
03:31 If statements
03:45 Arrays and slices
05:25 Maps
06:13 Loops
07:40 Functions
09:42 Structs
10:38 Pointers

MORE TUTORIALS
—————————————-

Learn Docker in 12 Minutes:
Docker Compose in 12 Minutes:
Deploy Docker Containers
Learn HTML in 12 Minutes:
Learn CSS in 12 Minutes:
Learn JavaScript in 12 Minutes:
Learn PHP in 15 Minutes:

SOCIAL MEDIA
—————————————-
twitter
facebook
instagram
snapchat jakewrightuk

go is a really popular language at the moment and for good reason it's a really great language to learn I'm gonna give you an overview of the language and by the end of the video you'll hopefully have the skills that you need to write simple programs if you go to golang.org you'll find a link to be able to download and install go if you're on Mac you can use brew install go if you find that easier once you've done that you'll have the gold command-line tools available when it comes to creating a go project you might find it a little different to what you're used to in other languages the idea and go is that all of your projects live in the same place and this place is called your workspace by default this is a folder called go in your home directory and you can see this if you type go and go path so I'm going to actually create this folder this is our workspace inside here we need another folder called SRC and this is where all of our source code is going to go so now inside of SRC you can create a folder for each of your projects let's make our first go file you'll often see them being called main goal but it can actually be called anything the first line in a goal file needs to be the name of the package every project at least if you want to be able to execute it needs to have a package called main the next section is the imports this is where you can import different packages there are loads of useful packages in the sunnat' library a common one is fmt which has functions related to input and output so open brackets and then you can list out all of the packages that you want to import note there are no commas in this list will just use fmt for now next let's look at functions you use the func keyword followed by the name of the function and the first one you'll want is a function called main it takes no arguments and it doesn't return anything but this is where the program starts a function called main inside of the package called main this will output hello world to the terminal to run this you can use go run followed by the name of the file if you want to compile the code into an executable then you can use go build in the programs directory so this time it doesn't run our code there's no output here in the terminal but what it has created is this binary file here this is an executable in Windows it's going to be a dot exe file and we can run that in the terminal alternatively you can use go install this is going to do something very similar except the executable is put in this bin folder it sits in our workspace next to the source folder and if this program had external dependencies so if we imported something from outside of the standard library it would compile and cache those dependencies into a package folder let's go back to our go file and look at variables to declare a variable you use the VAR keyword followed by the name of the variable and then the type so I'll make an int I can output this if we don't provide an initial value then the 0 value for the type will be used every type has a 0 value for integers the 0 value is literally just 0 for Strings for example the 0 value is the empty string I could later assign something to this variable or I could do it at the same time as declaring it so I could do this again and then add the two integers together so we get 12 when we give variables an initial value like we're doing here there's a shorthand syntax that we can use drop the VAR add a colon and an equals and then at the value and we can omit the type because go can infer it so this is the same as what we had before if I do this to all of them you can see we get the same results but this is a bit neater to read go has if Simmons they allow you to execute a block of code if a condition is true notice that there are no brackets around the condition there's also else if and else let's look at arrays an array holds multiple elements of the same type but the number of elements that an array holds is fixed it has a specific length for example we could create an array that holds 5 int if we output this we can see that it does hold 5 intz all of the elements will be initialized with their types 0 value again in the case events this is the number 0 we can use square brackets to index the race or we could set element at position 2 to be 7 arrays are zero index so the first element is at position as 0 if you want to be able to initialize the content of an array more easily we can use the shorthand syntax again so drop the var use a colon and an equals and at the end of the line opened curly brackets and then you can list out your values here now you might be thinking arrays with a fixed length sound inconvenient well they are if I wanted to add a sixth element to this array I can't because the length of the array is part of the erase type to overcome this you can use slices slices are an abstraction over the top of arrays to make them easier to work with namely they don't have a fixed number of elements so if we wanted to create a slice event instead of an array events we simply remove the element count and leave an empty set of square brackets so now this is a slice event so why is this useful well now we can use the built-in append function to add something new to the end of the slice append doesn't modify the original slice it returns a new one slices are backed by arrays so here go will be creating a new array in the background and copying stuff across but when you use slices you don't have to worry about it it all happens behind the scenes maps are really useful they hold key value pairs they're like dictionaries in Python or associative arrays in PHP the type definition from up is the word map and in square brackets the type of the keys followed by the type of the values in the map and to create a map you use the built in make function and give it this type we can set key value pairs using square brackets it's similar to indexing arrays you can use the same syntax to get the value for a particular key and you can use the delete function to remove something from the map so Square is now gone next loops the only type of loop in go is the for loop we declare and initialize a variable using the shorthand syntax we saw a couple of minutes ago this is the counter then we set the stopping condition so loop while I is less than 5 for example and then after another semicolon you can increment the counter or decremented or do whatever you want at the end of each iteration so this is going to go through the loop five times and print out the number the value of I in each iteration so it's after zero and it goes up to four and as soon as it gets to five it's not less than five anymore so two stops iteration the four loop also doubles as a while loop so if you delete the variable declaration from the beginning and the increment at the end you've got a while loop so this will do the same as before another thing you can do with the loop is iterate over each element in an array or a slice by using range we get the index and the value out of doing range of an array print line has a feature where you can pass multiple arguments to it and it'll output all of them separated by spaces you do the same thing with a map but you'll get the key instead of the index so far we've had everything in the main function but we could easily create a new function if you want to accept arguments you just need to give them a name under type and if you want a return value then you need to give the type of that after the brackets so this is going to take two ends and return a different int so we could do something really simple like this and then call it up here in goal functions can have multiple return values so if I made a square root function which takes us float 64 it can return another float 64 and it can also return an error these are all built-in types since the square roots of negative numbers are complex numbers we could limit this function to positive inputs only return in an error if X is less than zero so we want to return two values the first one is the float 64 we're not doing the calculation so let's return 0 and then we can use the new function in the errors package if I import that to create an error otherwise we can do the square root there's actually a square root function in the math package so I'll just defer to that we still need to return to values though and in this situation we don't want to return an aerosol is return nil instead of an error so now when we call our square root function we get two values we get the result and we get an error if the error is not nil then that means something went wrong so let's just output that otherwise output the result so does the square root of a positive number if we were to make this negative then it's going to return an error and we'll get this line executing instead so that's how it functions with multiple return values work it's really useful to be able to return errors like this because gold doesn't have exceptions if you're used to working with those in other languages its struct it's a collection of fields so you can group things together to create a more logical type to create a struct type you want to do this outside of a function use through a type followed by the name so this can be anything you want and then the word struct inside curly brackets you can list out the fields that you want the struct to have each field needs to have a name and a type so I'm gonna make this one a string and I'm gonna make this one an int it could be anything it could be another struct so that's the type to create a struct of that type you want to use the name of it and then just like when you initialize an array or a slice you can use curly brackets to set the fields so first I'd set the field called name I use a colon and then the value and if I out for this you will see it's created a struct if you want to get a specific field out of the struct then you can use dot notation to do that so this time it'll just output 23 the last thing I want to talk about is pointers if I have a variable as we've seen it's really easy to output that but we can also get the memory address of the variable by using an ampersand so this gives us a pointer to I now if I had a function which incremented a variable and recalled it like this this is going to be useless and not actually do anything because I is copied by value so the increment function gets a copy of I it increments that but then since we're not returning anything that copy is just discarded and the original variable is not modified if however we passed a pointer to the variable then the function is going to be able to look at the value at that memory reference and modify the original version so we can accept a pointer by prefixing the type with an asterisk we can send the pointer by using ampersand and what we need to do here is dereference the pointer so we use another asterisk here without that this is going to be incrementing the memory address but that's useless we want to see what's at that memory address using the asterisks dereference it and then increment that value so now this actually does modify the original eye variable very quick introduction to pointers I've lost over a lot of details here if you're unfamiliar with pointers it's definitely worth taking the time to read up on them properly but I hope this has been a good introduction to the language we've learned about functions variables if statement arrays slices maps loops errors and pointers it should be enough to get started leave a comment if you do have any questions if you're interested in a career in software development or you just want to improve your skills then you might find it useful to dive further into computer science there's a lot more to computer science than programming it's a very broad field that covers mathematical topics like linear algebra probability it covers Hardware algorithms and having an understanding of these topics can often greatly simplify a coding project brilliant org is a great place to learn more about computer science they offer curated courses on many things from the fundamentals all the way to cutting edge stuff like artificial neural networks the guided courses go into great detail to build up your knowledge and then walk you through problems to help you practice and really understand what you're learning understanding how memory works for example is going to help you write more efficient code and it'll help you reason about the code because you'll understand what's happening at the operating system on the CPU level if this sounds interesting got a brilliant org slash joke right the link is in the description you can sign up for free but the first 200 people who go to that link will get 20% off the annual premium subscription if you found this video useful click the like button hit subscribe if you want to see more tutorials like this one and I'll see you next time thanks for watching you

25 Replies to “Learn Go in 12 Minutes”

  1. The Freeze

    Why are all these new languages with slightly different syntax being made?

    They literally copy everything from other languages. Nil is literally just null in all other languages. Whats the point of changing something so unimportant?

  2. StormWolf01

    I've heard a lot about this language, but just by watching this video, i don't see what's special about it. But maybe that's the kind of language where you only understand its qualities when you use it?

  3. John Christy

    You need a decent understanding of all the principles of programming before you watch this 12 minute video. So more like explore go in 2 years and 12 minutes.

  4. Michael Murphy

    12 mins? Pssh! I don't have that kind of time. Please make a new video: "Learn go in 12 seconds"

    lol I'm kidding thank you for the video

Leave a Reply

Your email address will not be published. Required fields are marked *