Maps - Go Lang Practical Programming Tutorial p.14
Skills:
Tool Use & Function Calling80%
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
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: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Why I built a simple AI provider wrapper (and you might too)
Dev.to · zhongqiyue
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI