Go tutorial Part 10 (web app part 8)
Key Takeaways
This video tutorial demonstrates refactoring a web application using Go programming language, Go templating language, and JetBrains IDE, specifically GoGland, to improve efficiency and reusability of templates. It covers various aspects such as dynamic rendering, template parsing, and secure cookie handling.
Full Transcript
hi guys welcome to go tutorial part 10 my name is tensor from the tensor programming blog and today we're going to do some simple refactoring so we're gonna make our code a little bit leaner we're going to do things like for example we're gonna get rid of the base side HTML file make it so that we are calling our HTML templates in a more efficient way we're also going to completely remove our flash go file but keep the same functionality that we had before and we will be refactoring the way that we actually render the templates inside of our main deco file so here you see it says template part files where you reuse this code a lot of times writing reusable code is a big part of refactoring code and makes it a little bit easier to do things so that's what we will be trying to do in this tutorial before we get started I just wanted to point out that we are using a early version of Go gland which is a idea jetbrains IDE that they just released for go thus far I haven't hit any bugs with it but you never know if we do encounter any bugs that would be the reason why if any of you guys are interested in getting in on these early editions I'll leave a link in the description I'm really actually quite liking this IDE ok so let's get started so the first thing we want to do is actually change the way our templates work currently we have this skeleton template the base template then we have an index template which has our header and footer inside of it as well as a body template which has a block in it and every time we render our template basically what happens is the page that we go to for example will say we go to our login page we get this block rendered inside of our index block here at this main block here and then this all gets rendered inside of our based on HTML in these areas here so what we want to do is we want to cut all of this out so the base that HTML we want to remove all of this so that we can call each of our main templates individually so when we go to our internal that HTML file we want to be able to call it but still get the header and footer is out of the page that we go to and we want to do the same for our sign up and for our main we also want to completely remove the flesh that HTML file because we can actually do this quite a little bit better so we can get rid of the flash at HTML file with a feature of the go templating language that we haven't discussed yet so I'm gonna just go ahead and delete the file and we do this by going into our main dot HTML where we actually call the block here and I've copied all of the stuff that was in the flash out HTML and I'm just gonna paste it right here where that block was so now we've got our little div element here and we've got a printf statement with a s and it's pressing in whatever data that's coming into this template so the way this is going to work is we are going to make a statement here and we're going to actually turn this entire thing into a block so we're gonna write end here and we're going to use a method called width we're gonna say with dot so currently if there is any data it will render this part of the template but if we specify with errors then it will only render this template or this part of the template if we have a type of data that we're passing through that's called dot errors and I'll show you how we actually implement that later so now let's actually redefine this template so we don't want it to be main we want it to be a more unique modifier so in this case we'll just call it sign in make sense because this is our sign-in form and we want to import our header and our footer so we're going to bring those in as well so there you go now we're importing our header and our footer templates into this we also want to make a change to our header and footer templates so that they have all of the HTML in them so if we go into this flower we can pull this part out we can put it in our footer so we can put the HTML body tags and the HTML tag at the bottom of our footer here then we can take this little footer here and we can rip it out of this block here and put it down here we're in our footer and completely remove this body block because we won't need it and now we can go into our base HTML take out this tag put it at the bottom of our header and then take out these tags and place them at the ball at the top of our header so now this index.html file only has our header and our footer template in it it doesn't have a body template in it anymore we can actually delete this base that HTML file so now let's go and change our internal that HTML to be like our main dot HTML where it's bringing the header and footer so this template is now called internal and now it's bringing in the header and the footer and finally we need to do the same for our signup so we will call this signup and we were bringing both the header and the footer currently if we run our program it's not going to actually work because we don't actually have a base template every time we execute our template here we're calling our base template and since our base side HTML was our base template this won't work anymore so we need to do our refactor we want to create a function called render and this function is going to allow us to basically execute our templates so we're going to pass in the HTTP response writer we're going to pass in a name string and we're going to pass in a data interface we're going to check for an error in this case we'll say template parse and we're gonna say parse glob and the way this function works is it takes in a reg X pattern and then it parses all of the templates that are based on that pattern so in this case we can just put in a star dot HTML and this will parse any file inside of this user folder we start at HTML next to it and it'll parse all of them together and actually that will work for us because we will be calling each individual template so now we can do our error checking we can now say template execute template and we are going to call W and then name we're gonna pass the name string in here and then we're gonna pass the data interface in so we can actually pass render W sign in and message into here and then into this part this else part we can pass in render W sign in and you so we're passing in the user that we create right here and as you can see this actually cleans up our code quite a bit so now let's go to where we are also creating more templates so for example in the example page right here we can remove this entire piece and we can also use and remove this entire piece here as well as the error-checking so we can say render and this is our example page so we're gonna call internal which is the template for our internal dot HTML so as you can see we're calling the name of each template and that's how it's working and then we can pass in you which will be a instance of user so there we go we've created an instance of user right here on the spot and we're setting it in to render and finally we need to do it for our signup form here so let's remove this and remove this and if we put right here render W and pass in sign up and then you so this is all well and good and it makes things a lot cleaner but one problem we will run into is with this here so we're passing a string in here but because of the way that we've set up our frost message in main this will actually not render so let me show you what I'm talking about ok so we're at our login form and as you can see it's not rendering but if we go to our signup it should work perfectly so we're having a problem because of that message so we could go back in and comment that out or we could change this structure so that it works properly so the way we're going to do this is we're actually going to go into a data structure here and we're going to create a new field in this field it's going to be called error or it's going to be called errors and it's going to take in a map with two strings bodies so the key will be a string and the value will be a string type we're going to do this for a reason that will become clear later on in the tutorial ok so the way that we circumvent the problem with our message variable here is that we define a variable u that is equal to an instance of our struct of user and then we say specifically that our you dot errors which means the you errors field equals a map of string and string and then we say u dot errors message equals string message so we convert the message into a string and then we set it equal to the value of message so if we run this as you can see the formatting is wrong but our message is popping up so we can actually reload it and it goes away and pops up the way that we actually correct this so that we only get the message we can just go into our main dot HTML and we can put a dot here and then type in message which is the field of the actual map so as long as you put the map value in here after the original field it should work so if we save all this and then we reload it there we go we're just getting the string that we want so one other quick thing that we did is we actually changed the way our sign up worked before we had each of our form values being set to a variable and then we set the variables inside of our user instead what we're doing here is we're putting our form values into our user directly so we're calling each of the fields and then we're sending them equal to the form value this is a much more succinct way of doing things and we don't have to set aside any more memory to create these temporary variables so the other big thing that we want to do in this tutorial is we want to get rid of our flash like go file so currently we are encoding and decoding cookies using this basic C for a URL encoding library in this particular file and then inside of our cookie Daigo file we are using our secure cookies encode and decode to do exactly the same thing so it's sort of redundant that we have both of these so what we're going to do is we're actually going to recreate our get message and set message functions inside of our cookies file so the way we do this is actually very easy so our set message is going to be very simple or very similar to our set session function in fact we can just copy most of this here paste it in here and just change a few things so for example if we remove this part here and we change this entire string to name and then we can change this part to our message so now we're just putting in our little name and message and of course we can change this from a byte to a string to make things simpler and then we say if encoded cookie Handler and this should become name and the value stays the same and then in here we're going to write name as well and that's all we need to do for a set message then for our get message we pour our get username function here copy this and put it inside of our get message here we can then change all of this so let's remove this part here and we want to actually return message of type string so this will be now a global variable inside of this function and we will change this part here the session part to name and this is creating a map here and then we're taking the map and we're decoding it from our cookie so we are going to replace this with name and then we want to call the name field of our cookie and set it equal to MSG and then we're going to return MSG so this is exactly the same as what we were doing in our flesh that go file except now we're using the encode and decode from our secure cookie library there is one more little edit that we need to make and that is with our clear sessions function here we want to pass in a name of type string in here so that we can actually call on the session that we want to clear so when we clear a cookie we can say ok we want to clear this specific cookie and we're gonna call clear session right here at the end of get message so we're gonna say clear session and then we're gonna type in name and what this will do is it'll allow us to get the message from the cookie and remove the cookie we also of course need to pass in W so we just say clear session W and name so after we get the the message from our cookie value then we delete the cookie and then we just return the message so then we need to amend everywhere else that we have clear session which is inside our main deco file so here's clear session we just need to pass in a name right here in this case we're just going to hard-code session in here also we're going to need to remove the slice of byte modifiers here because these are being passed in as strings now rather than bytes and we're also going to need to do the same for here as well first of all this is not becoming a string and strings can't be nil and go instead they can only be empty strings we're only returning one variable in this case message so we're not returning an error and a message are only returning a message so all of this should now work quite nicely so now we only need to run three files when we spin up our server and there we go so this is all working really quite nicely alright guys well I hope you enjoyed this tutorial I know it was not exactly the most enlightening of tutorials we did kind of go over some new cool things inside of the go templating engine and we also talked about how we can keep things consistent and how we can sort of render things differently for our templates in our next tutorial we're gonna talk about how we can hash our password as well as create our unique user ID anyway guys if you enjoyed this tutorial please feel free to like and subscribe if you have any questions of course feel free to comment and if you dislike this video as always feel free to dislike it I hope you guys had a good holidays and I hope you guys have a good new year
Original Description
In this tutorial, we do a bit of a refactor job on our webapp to make things easier going forward.
Gogland link: https://www.jetbrains.com/go/
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-10
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 · 18 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▶
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
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 Reads
📰
📰
📰
📰
Practical Ways Small Businesses Can Use AI to Automate Work and Improve Efficiency
Medium · DevOps
VaultSort REVIEW AI-Powered File Organization and Secure Storage Management for Mac
Medium · Machine Learning
Build a Self-Improving Second Brain on Oracle AI Database
Dev.to · Anya Summers
The Circuit Boards Keeping the New Space Race Alive
Medium · AI
🎓
Tutor Explanation
DeepCamp AI