Types - Go Lang Practical Programming Tutorial p.3
Key Takeaways
The video tutorial covers the basics of types in Go programming language, including explicit type declaration, built-in types, and type inference. It demonstrates how to declare variables, use shorthand, and define constants in Go.
Full Transcript
what is going on everybody and welcome to part three of the go tutorial basics in this tutorial what were to be talking about is typing information like I said in the first tutorial we really are gonna get to actually applying this to something more useful the thing we're gonna apply it to though is go has a built-in web server does web obviously like web applications pretty simply right out of the box even with just the standard library so that's what we're gonna do but even just to do the most basic hello world example we still have to get through a lot of like really basic truly just basic things about go especially because I think a lot of people are coming from Python since that's mainly what I do on my channel and things like today what we're gonna be talking about is typing information which is something you just don't even have to think about in Python there's some little intricacies here that will trip you up so anyways let's go ahead and get into it so I'm going to for this one we just really need format and we can empty out main and let's talk about types so in go there's quite a few types I'll put a link in the description to the text-based version of this tutorial if I forget someone just remind me and there it has all the types there but you will use those and then you can actually create your own types later on basically the types that you're going to be using here or gonna be like bool int float 32 or float 64 byte I'm probably forgetting some of the other ones that you're probably going to use string finance a string anyway but you can check out all the types that you have available we're gonna use mostly just numbers for now just to get an idea we'll probably throw a string in there too but anyway let's go ahead and create a simple function we're gonna call this funk add an add is gonna add together two variables like x and y and then it's gonna return a whatever the addition of these two things are so so in Python you wouldn't have to add any typing but and go you have to add what are the types of these these variables so X the type of X we're going to say is float 64 and then for y we're going to say this thank float64 also you need to specify the type if the function is going to return something you have specify of what type will the return be again we're gonna say float 64 now since go is all about efficiency it's likely the case that you don't need the precision of 64 bits so you could probably use the float 32 but I'm a man with the plan and I'm using float64 for a reason so we'll continue with float 64 and you'll see why soon so return and then we just return whatever X + + y is so then we can come down here and we could add some things together but I also want to show you variables so we're gonna we're going to define some variables and then pass those variables to the function because we could just say add and then you know 55.5 in something else but we reuse variables first so to define a variable there's a few ways we could get away with this or and do this but we'll just show a really basic one first so let's say we're gonna call this number 1 so it's a var num 1 so far the name of it and then the type that we're gonna make this so this will be a float 64 because this is expecting the float 64 and then we'll say that equals 5.6 and then we're going to save our num2 again below 64 and that's going to be equal to nine point five now we could do format dot print capital P print line and then add num1 num2 so we'll save that and then we'll just pull up the interpreter go run go to go and we get 15 point 1 now chances are you're not going to see people's programs that look like this they're going to use a lot more flex or hand so there's a lot of things that we can do to make this quicker in unless text on screen so first of all anytime you've got a succession of things just in general in programming if you've got like repetition you there's probably a way to get rid of the repetition so so anytime you've got a bunch of like a bunch of variables that are the same type or parameters of the same exact type you can actually just condense it so we can get rid of this float64 here and just say X comma Y float64 function returns a float 64 return X plus y let's just save and run that real quick just to show cool as expected now the next thing is like the we've got multiple variables here now the same thing you could do in Python you can do here so you could say var num1 comma 9 to both of type float 64 equals 5.6 comma 9.5 so two things it'll get unpacked into those like gnome 1 num2 so 5.6 gets unpacked to num1 num2 isn't 9.5 and so on so we could save that run that again 15 1 that said people don't tend to do this in go so anything outside of a function you would have to define using the var and give the type and all that if it's inside a function you can use some shorthand and you actually don't have to give the type go will figure it out when you compile but the type cannot change after it's been compiled so it'll kind of figure it out and then once it compiles you could never change that type again so you still have to it's only gonna have one type it's not going to be dynamic every time the program comes to it it's just one time so what you could do is you could say rather than fari you could just say num1 comma mom to get rid of float64 colon equals five point six nine point five now we can add those two things together so save come up here run and we get fifteen point one now like I said before man with the plan what if we wanted this to be super efficient because five point six nine point five we don't need the precision of 64 bits let's go to 32 less garbage so we'll go ahead and save that and let's run that but we get there and the air is that we can't use num1 which happens to be a type float64 as a type float32 in argument blah blah blah so when when this was was given a type go saw okay it's a float we'll just give it float 64 and then it gets through the function here and it's like up we can't use that right so so yes you can use the shorthand but if you wanted things to be explicit in some way and you want to use flick float 32 for example and not just some default type that it's gonna get you would need to specify typing information also just in case I don't cover it there are also constant so CO and St obviously to notify that it's a constant you have to use the term constant so you've got to define it like the tradition of the first variables I showed you so yeah like constant I don't know X in steepness watch okay so okay those that's a few things there now with shorthand like let's say you had a punk that's going to return multiple things so we're gonna say multiple and then multiple is gonna take in will say two streams so let's say it takes an a B string and then it returns which is kind of it return a B so it returns string right but since we want this function to be able to do return a comma B ie its returning two things so two strings we actually unfortunately have to put this again in parentheses watch out there and specify every return type even if they are the same return type you still have to specify both of them so a B string string so now what we're gonna do and I'm gonna purposely leave them wanting them to there for now just to show you another interesting thing about go so now let's say we'll say word one comma word 2 equals hey I already messed up but oh I messed up twice two double quotes first of all and then also not a saying equals colon equals will say hey comma there okay and then we'll go ahead and format dot prints don't forget your capital P print line and then whatever the return of multiple is when we pass w1 and w2 so which I just say hey there but we're gonna hit there we'll go ahead and run it anyway and there you go you see num1 is declared not used and num2 this is interesting it kind of makes development sometimes hard as you're trying to build a program in and output things as you go so if you really wanted them wanting them to you like you're planning to use it but you just wanted to make sure you got to a certain point you could just comment them out so a comment is just a single line comment it's just gonna be two slashes a double line comment is gonna be you know forward slash star or asterisk asterisk florid slash and that that would work over multiple lines okay so you could just comment them out if you wanted to use them but we're pretty much done with them now so i'm gonna go ahead and delete them now the function that you wrote is and it's not a big deal but also if you made an import that you don't use that's gonna throw an error so we'll save that and we should see hey there this time and we do that's good so there's that so now a couple more things i want to show before i wrap this one up is like you can do like if you wanted to convert a type like you could save bar a int B equals 62 and like you want to say like Barbie float 64 equals and then you can just convert it so you could say float64 a and that will convert a to a float at 64 also you can they type inference works so you could say like we've got VAR a int 62 you could then say X Colt loops X colon equals a and X will be type int okay so you can kind of just keep that in the back your mind so I think that's all I want to show on typing information and just one more basic thing that you've got to get used to especially if you're coming from Python you've never had to think about that if you have questions comments concerns whatever feel free to leave them below otherwise I will see you in the next tutorial
Original Description
Welcome to part 3 of the Go programming tutorial series. In the previous tutorial, we covered some basics on imports and functions. Next, we're going to talk a bit about types in Go. As mentioned in the first tutorial, Go is a static-typed language, meaning you need to specify the type, and the type cannot change without you explicitly changing it.
Text tutorials and sample code: https://pythonprogramming.net/go/
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 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
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI