Maps - Go Lang Practical Programming Tutorial p.14

sentdex · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

The video covers the basics of maps in the Go programming language, including defining, initializing, and using maps to store key-value pairs, as well as iterating over maps and removing values. The tutorial uses a simple example of storing student grades in a map.

Full Transcript

what's going on everybody welcome to part 14 of our to go language to real series in this tutorial what we're going to be talking about is the basics of Maps so in the go programming language if you want to store something in the sort of you know key and values system the way that you're gonna do that is with maps so let's just go over some basic examples this should be a pretty quick tutorial and then we'll actually apply it to a real use case in the next tutorial with our news aggregator web app so to begin let's just start we're just gonna have a fresh script here just because it should be pretty simple I'm gonna go ahead and import and actually we're just gonna use format so I'll just import format also yeah let's - let's just do func main and then we'll just do everything in here so a typical map is gonna be defined like so so like you could say var grades or something like that and then map and then this will be a map containing basically strings or basically it'll be a string key and then the value will be float32 okay for example so and actually in our case well we'll go with float 32 that's fine I'm probably gonna make floats but anyway so this would be a grades map right so in theory you know you might have students names to their grades in your class okay so now obviously we don't need this since we're inside of a function so you'd probably have something more along the lines of grades colon equals but also a map is just a reference type so it actually doesn't have any values or anything like that if you want to have it have values you need to use goes make so you're just going to encase this in make and that's actually gonna go ahead and initialize it for you and all that and then what we can do is start to actually add values to it and get values from and all that fun stuff so let's go ahead now that that's done what we can do is we can start adding things and it's just like a Python dictionary so basically the way that you do that is you just say grades and then don't forget to do double quotes I always want to do single close Timmy and yes that's his real name it's like you know how hipsters are doing it these days it's like they're given like nicknames real names so anyways yeah Timmy I don't know not my kid anyways he got 42 and guess that nickname didn't help him very much in school and then let's go ahead and give a few more we're gonna do Jess again what's going on here anyway Jess got 92 so good good for Jess and then finally let's just add one more and then let's say this is Sam and Sam got a 67 okay so we have that and then now what we can do is format dot print line we can just we can print all of grades so I'll just go ahead and save that and then we'll bring this up go run go touch go cool so as you can see here that's just the full map and now generally you're probably gonna like print out your whole map but you can if you want also what we can do is we can begin to like we can take values and assign them to specific variables so we could say like I don't know Tim's grade oh you know what we should probably do it this way Tim's Tim's grade people are getting angry so so we found that the style gods from my tutorials so some people were pointing out first of all like these this would be you know your styles for Python if you're gonna give a variable but in go first of all you probably wanna capitalized exported most likely and then yeah do like basically title casing yeah cool also just for the record you can also do go format so go fmt and then your actual scripts and then basically what this will do is it'll kind of like fix all your all your style mistakes for you so if you're if you're not if you're someone like me who doesn't really pay much attention to it maybe that's your new best friend I don't know anyway back to the tutorial oh sorry Tim's grade colon equals grades Timmy and for that matter probably grades should be capitalized to anyway we'll use it as it will assume that for some reason we wanted it to be internal so so so now we can do that and then we could say yeah format print line let's print Tim's braid we'll say that come back up here go run go tight and so we got the full map because we were printing that out but also we got a 42 there unfortunately because Tim was doing so poorly in our class he's been he's been dropped from the class basically we're pushing him back a grade it's a really sad situation but anyways if you want to remove something you can just use the delete syntax so just delete and then delete from where we're gonna delete from grades and then what are we gonna delete well we're we're deleting all little Timmy good bye Timmy and then what we can do is let's just cut and paste down here we'll save that and let's run this one more time go run go touch go so now as you can see Tim is no longer with us finally the last thing that we can do is not have multiple cursors cool and what we're going to do now is iterate through a map I almost called it a dictionary anyway which is probably a common task that you're gonna need to do so when you iterate through this is kind of basically you're gonna use the the range keyword I'm pretty sure we've used range already but yeah we have so before when we used range it returned in index and a value right and we just took the index and we just used underscore basically because if you wanted the index cool now in the case of a dictionary a dictionary already like if you wanted to iterate over that dictionary in theory it could are like in Python when you iterate over it I said dictionary anyway I was gonna do that map in Python if you were to iterate over a dictionary you would get back just like the key and then if you wanted the value do the dictionary key thing but if you iterate over a map and go language it will return you can return both the key and the values so for example you would say for K comma V so key value in range grades we can iterate over that so now we could just say format print line K well we could just say well let's do [Music] let's do K sorry it's under case k and then colon comma V so it'll be like the student's name and then their grade something like that let's go ahead and come up here go run go type duck go cool and then basically we've iterated over it and then now yeah you've you've got the the students name of the grade okay pretty cool but obviously super simple example the other thing that you'll probably notice in that we're going to kind of exemplify in the next tutorial is like it doesn't appear super simple like how what if what if we wanted more than just a float 32 here like what if we wanted multiple values could we add multiple values like for example could we get away with I don't know throwing in you know a list or a - you know like like a float 32 inch string or int and float 32 and so on and no so so what if you do want to have multiple values there well as you've seen so far structs are your best friend in golang so actually you would just create your own type that might have multiple values and that's what you passed there if you want to do that which is something we are going to have to do in the next tutorial so anyways if you have questions comments concerns on maps leaving below otherwise I will see you in the next tutorial

Original Description

Welcome to part 14 of the Go programming tutorial series, where we're going to cover maps. In the previous tutorial, we've been working on parsing XML sitemaps for data on articles. In our case, we've got titles, keywords, and URLs that link to the articles. We'd like to be able to store these into maps like: Title: [keywords, url]. 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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches the basics of maps in Go, including definition, initialization, and usage, with a simple example of storing student grades. It covers iteration and removal of values, and introduces the concept of using structs for multiple values.

Key Takeaways
  1. Define a map with a specific key and value type
  2. Initialize a map using the make function
  3. Add key-value pairs to a map
  4. Iterate over a map using the range keyword
  5. Remove values from a map using the delete function
  6. Use structs to store multiple values in a map
💡 Maps in Go are reference types and need to be initialized using the make function to store values. Structs can be used to store multiple values in a map.

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →