Go tutorial Part 14 (Web tutorial part 12)
Skills:
Tool Use & Function Calling80%Prompt Craft60%Advanced Prompting60%Prompt Systems Engineering60%
Key Takeaways
This video tutorial demonstrates how to refactor a User module with a main web app in Go, utilizing tools such as IntelliJ, SQLite, and gorilla/mux router, and covering concepts like database management, template rendering, and authentication.
Full Transcript
hi guys welcome to go tutorial part 14 my name is tensor from the tensor programming blog so in this tutorial we are going to merge our user module with our web module so before we get started I might as well just mention I know it's been a about a week since I've made a video and this is mainly because of personal reasons there are a few unfortunate circumstances that prevented us from actually going forward with some of these videos but now those have been cleared up and things should return to normal let's get started here as you can see here in our project folder we have our original main web app here it has the upload the test module all that stuff inside of it and this is just our one file it's got a bunch of stuff like our page save cache file which you know should be in a database folder and then we've got our templates up here our database create database string we have a load function and a load source function and all these are mainly for the back end or the really far back in romera inside of this folder our user folder we've got our user module that we've been working on for the past six or so tutorials and as you can see we've got our main or data and our cookie files so the first thing we want to do is bring our data and cookie files up into our root folder here so we're gonna get our data and cookie files as well as our index internal main and sign up HTML files we're just gonna bring them up into web so we've brought all them into this folder let's close this real quick and our index file was replaced with the one from our our user module but the other files just came out exactly the same so we have our base HTML file which we are going to delete right now because we won't need it anymore got our index.html file or our edit HTML file and we're going to rename this to edit and then we are going to add a few things here so we're going to say template editor and we are going to also add our navbar and then at the end of it we're going to add our footer and we are going to do this for all of our old HTML file so we're going to do it for tests as well though for tests we're going to rename it let's just rename it this test right now and upload we're going to rename his upload and so the header and footer and the navbar and we need to make sure of course to put our footer and test as well let's take our struct our page struct out of our main function and put it in our data file let's remove these functions here so load source load and save cache are going to go into our data die go of course make sure you get the imports correctly as you can see here we're having an issue with string version import so I'll just go ahead and import that and within with IntelliJ is go gland I can just ask to import it that way if not you just type in you know parentheses string cover we are going to also have to move our database variable as well as our create database variable into our database file so we will save our and paste these in so another issue that we're having is of course we're using this variable which is leaving our SQL database open the entire time we're running our app this is not generally something that we want it's something that works while we were just making our application so to change that we're going to sort of put this kind of logic inside of these three functions like what we've done here we're just going to copy and paste all this in so let's do this here to do this here and do this here and we do kind of want to have the same database for everything we're going to change it from users that SQLite to something else though so let's actually remove this here because we don't need it anymore and let's copy and paste the string and we're going to directly input it in where we are using the variable instead so there we go so now we can remove this variable and finally let's refactor our users SQLite and let's just call it our DB on every single instance just go through and change users to DB all right and we can also if we wanted to we could add our cache folder to this as well so that we put our database inside the cache folder every time and actually that's what I'm going to go ahead and do so I'm going to put in cache back slash and then the DB name so now if we're going to remain folder we need to remove all the imports that we're not using in here anymore like these three or four rather and we are actually going to remove these templates eventually here so let's go into our main die go for our user module rather and first of all let's leave the imports alone we need to take out the cookie handler so we need to take that and move that over to our main die go so we'll just put it up here for now in my case it will automatically import gorillas secure cookie and then we will go back into our main and we want to take all of our handler functions here as well as our render function copy and paste them into our main die go file let's put them all below these ones and as you can see we're getting a few errors so for example our go validator is not being imported let's go back into our main user and import our go validator so here is the important just copy this and move it over so now we need to actually create the handlers in our main function to go back into our user main function we can remove these which are the handlers so let's copy these and put them into this main file and then one more thing that we want to do is we need this go validator set fields required by default let's put that in here as you can see we're getting errors from our router we want to change all of these to HTTP so that they use the default router now and what this will do is it will actually make these methods become errors which we will have to remove which is fine now the reason we're not going to use the gorilla mucks router anymore it's because we don't really want to for this particular application the reason why we even brought it in the first place was just to show you that you do have alternatives now everything should be working perfectly fine though there are some minor things that we need to change before we start running things so for example we've changed the way that our templates work so we can completely remove these and in the same vein we want to call our render function so we want to say render W and in this case we're going to take in tests because we want the test template to run and then we're going to input P as our page and then down here we're going to do the same thing except instead of test we're going to put in edit and then finally right here in our upload we want to render W and we want to say upload and people let's run this so here we are at our localhost 8009 in our signup brother so let's put in a name tensor with a capital T first name John Doe email will be test at test comm and our password doesn't really matter as long as they match submit them tensor submit the password and here we go so now we're logged in we have our test page so we can click this little link that I've created test page we have our edit page and we can go to our upload file but here's one of the big problem we can log out but we can still manually go into test test and upload and even head it and the reason this works is because we're not actually using our validation to check and see if the user is logged in to try and get to these pages so we want to correct so we're going to say UUID equals ya you UID and we're going to pass in the request and then we're going to check to see if our unique user eggsy exists so we're gonna say if you UID not equal n to string then we will do all of this stuff so this will get the title and then it will render the page if this doesn't work then we want to redirect back to our index as you can see we're running HTTP redirect passing in wnr and redirecting back to the typical index which is our login form so we want to add a return statement here so that we don't have a header error and we'll also put a return statement here even though it shouldn't matter so we want to add the same to our edit function here so the same type of functionality and to our view function here all right so now if we rerun all this stuff if we don't log in we shouldn't be able to get to our test and our edit and our upload page which is how it should be of course so without the cookie you won't be able to get anywhere inside of our application so here if we go to edit and test you to automatically redirect us to login let's login with tensor and now we can freely go to upload edit and test and if we log out and let's say we want to try and go to test again it doesn't work another thing that we want to add is we want to add the ability for the page to automatically redirect from the login form if the user is already logged in what I mean live by this is if the user has Earth the cookie has already been issued the user doesn't really want to be able to see the login form right so now we're going to check and see if we have our unique user ID here on the login form but unlike our other ones where we you know actually rendered the page we want the page to render run our unique user ID doesn't exist rather than copy and paste all this inside of here we want to actually just redirect here so we won't say HTTP redirect WR and let's redirect to our example page and we'll put our return statement here oh I'm sorry we put this in the wrong hand l'heure that's why so this is the post handler to login this is not our actual login form we need to find where we're actually rendering our login form so here's our internal page and then right here we have our sign in form so here our index page is where we want this logic so now let's rerun everything alright so now we've logged in and if we want to go back to the login page it automatically redirect suspect to example and if we go to the signup page it should work which is nice but if we try to go back to our login page it will automatically redirect us yet again so to get back to the login page we have to log out now the reason we're doing this is to make it so that if a person logs in and say they close their browser and they come back and the cookies still exists then they will automatically log in before we go we're gonna do one more thing and we're going to create a directory it's gonna be called templates we're gonna take all of our HTML files and we're gonna put them inside of it alright so now our template files has all of our templates in it so now because of the way that we're rendering our templates with our render function because we're using parse CLOB we can just type in template and then a backslash so it'll go into our templates folder and render all of these at once and then based on the handler that we choose to go to it will pick which file to render make sure to make the name of templates plural or actually the same as your folder otherwise you're gonna run into a huge error here like I just did let's clear it and they're on it real quick alright so now everything is working perfectly so if we go to tensor and we type in our password it should work and now if we go to our test page let's actually create these in the database so we'll get our test and our edit page and we'll save them and so now we've created them both in the database so our database now is inside of cache it's it's this DB SQLite we can delete this web DB and let's actually bring out our SQLite browser so as you can see here our database now has two tables we have our pages and our user table so all these are working quite nicely and as you can see our pages has our two pages that we just saved our user page has just the user that we created as well as the unique user ID in the hashed password so everything seems to be working quite well all right guys I hope you enjoyed this tutorial I know it wasn't anything revolutionary we didn't really do anything new we just kind of refactored everything and combined everything together in the next tutorial we're going to do a little bit more refactoring though it's going to be something that we haven't done before we're going to obviously trim down this page as you can see we've got a pretty long file here we want to make all of our files pretty short if we can so we're going to learn how we can do that so if you enjoyed our tutorial please feel free to subscribe and like and if you have any questions of course feel free to comment if you dislike this tutorial then by all means you can down vote and harass us as much as you want make sure to check us out on Twitter just recently we had a vote to see what language we should do as our next tutorial set and the winner of that was rust now one of the things about rust that makes me a little hesitant to do tutorials on it is the fact that every six weeks the version number changes so I may run another poll to see if you know people want to see some other language instead or I may choose to just go with a lick sir for now and wait until Russ gets a little bit more stable anyway if you guys have any comments on that please feel free to leave them in the comment section alright guys have a good day
Original Description
In this tutorial, we refactor our User module with our main web app. We also add some minor features.
Also check out our written Golang tutorial on our blog: http://tensor-programming.com/welcome-to-go/
Check out the Code on Github: https://github.com/tensor-programming/go-tutorial-14
Check out our Twitter: https://twitter.com/TensorProgram
Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tensor Programming · Tensor Programming · 23 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
▶
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
NodeJs, Text editors and IDEs
Tensor Programming
Vanilla JS todo App
Tensor Programming
Elm Tutorial part 1
Tensor Programming
Elm Lang Tutorial, Part 2
Tensor Programming
Elm Tutorial Part 3
Tensor Programming
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
Elm Tutorial part 5 -- Snake Game
Tensor Programming
Elm Tutorial part 6 -- Calculator
Tensor Programming
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
Go Tutorial part 2 -- Web Crawler
Tensor Programming
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
Go tutorial part 5 (web app part 3)
Tensor Programming
Go tutorial part 6 (webapp part 4)
Tensor Programming
Go tutorial part 7 (web app part 5)
Tensor Programming
Go tutorial part 8 (Web app part 6)
Tensor Programming
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
Looking at Elm 0.18
Tensor Programming
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
Go tutorial part 16 (web app part 14)
Tensor Programming
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Tensor Programming
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
elixir tutorial part 1
Tensor Programming
elixir tutorial part 2
Tensor Programming
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
A Intro to Clojure and Clojure Syntax
Tensor Programming
An Update about the channel
Tensor Programming
Intro to Rustlang (Setup and Primitives)
Tensor Programming
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
Intro to RustLang (Enums and Options)
Tensor Programming
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
Rustlang Project: Snake Game
Tensor Programming
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
Intro to Rust-lang (Error Handling)
Tensor Programming
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
Rustlang Project: Port Sniffer CLI
Tensor Programming
Rustlang Project: Chat Application
Tensor Programming
Rustlang Project: CLI Toy Blockchain
Tensor Programming
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming
More on: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI