Go tutorial part 17 (web app part 15)

Tensor Programming · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

This video tutorial covers adding multi-word titles to a program and customizing a function in the template language in Go, utilizing tools like Go 1.8, regX 101.com, and SQLite.

Full Transcript

hi guys welcome to go tutorial part 17 my name is tensor from the tensor programming blog and today we are going to modify our web app to allow for multi worded titles before we get started I just thought I'd mentioned that go 1.8 just recently came out now there aren't any huge major syntactic changes to go 1.8 the biggest changes that were made were mostly to optimization and performance if you do decide to go ahead and install 1.8 there are a few things that you should do for example on your go path you'll notice that you have a folder called pkg inside of this folder you have a bunch of your dependencies and they've been compiled to these des files and this basically allows your go projects to run really quickly so they don't need to be recompiled every single time you're running your projects and stuff so what you want to do is when you update to 1.8 from 1.7 you want to delete this entire folder so that when you do actually rebuild your projects they will get built from scratch because some of the dot a files will not be optimized for 1.8 and this will cause slowdowns and stuff anyway so let's start working on this project here so our goal here is to add multi word titles to our file if you remember in our create function here we actually have a statement here that says if our string contains a space in it then we just redirect back to create so that way people can't create a title with a space in it so instead of that we're going to actually modify this but before we do that we need to actually go down to our closure here our check path closure and we need to talk about how this is working so currently with our check path closure we have this path variable and the way that we're getting the path variable is we are checking to see if this r dot or l dot path is matching with our reg x which is up top and so the r dot URL dot path is actually a slice of strings and it comes from our URL path so this is what it would look like so for example here is a URL and this is what our actual output will look like so our path variable will output this sort of array and it will have each of the strings will be divided it where the back slashes are so this is what it look like it'll say localhost 8,000 and that's why we're passing in path index 2 because index 2 is where this test will be so when we actually extend this to two pages so for example if we just type in test page and say that that's our URL problem is that there's now a space here and of course in a URL you don't want a space even in here we have a space and that's just going to get confusing so instead of having a space we want to have an underscore so we want it to basically look like this we want localhost 8080 is we need to allow underscores to actually happen inside of our reg X now there's this nice little website called the reg X 101.com and it allows us to actually take a look at what a reg X would look like and what matches our regular expression so if we copy and paste our regular expression in here you'll actually see that we have what are called - capturing groups so the first capturing group is either edit save or test so if we type in edit first we have to type in a backslash so we type in backslash edit and then we type in any word after it so this will match even though it's a bunch of gobbledygook but as soon as we put an underscore in here it doesn't match so we need to figure out okay well how can we modify this to add an underscore well there are two ways with regular expressions one of them is to literally put an underscore here so if we put an underscore inside the brackets it will literally say okay look for underscores and as you can see edit how our world is now working but the other way to do it is a little bit more succinct and that is to replace all of this with colon backslash W plus : now what this is doing is it's saying okay match any word character which is equal to a to z uppercase a to Z or 0 to 9 and then underscore and we can match that as many times as we want so let's copy this into our application so if we copy it into our application we're gonna get an error here and the reason is is because we're using double quotes we have to use back quotes so the single back quotes and that will fix it now the problem came from this backslash of course that's an escaping character in double quotes and go so now we fixed our reg ex to account for underscores now we need to actually create the underscores so let's go down to our create method and let's fix this here so the way to do this in go is to just use a function called string start replace to replace all the spaces with underscores so we're gonna say title equals string start replace then we put in title that we put in the space that we want to replace then we put in underscore which we want to replace it with and then we put in negative 1 which is the index and that basically says iterate through the entire string find all the spaces and replace them with underscores so this is fine for our create function but of course we need to add a few more things to our file so let's actually go back into our data that go file and modify it a little bit so we want to actually save our title inside of our database with the underscores not the spaces so of course we can put that if statement in here as well so if we just put in the if string that contains and then we instead of title this will be P dot title and we replace these titles with PETA title this will not only save our PETA title with underscores inside of our SQLite database it will also create a file name with underscores instead of spaces so now let's go back into our main deco file and now we need to append a few of our functions so in particular we want to our search function so when somebody searches for a function the way it works is that we use this page exist function to check inside of our database and match the actual string that we're sending in here and we've actually got a redundancy here so let's remove this when we send this in there we need to match the two together so we want to say okay s dot value equals strings dot replace and we want to replace all the S value spaces with underscores like we were doing about now the reason we're not using an if statement here is because it really doesn't matter so with our view and our edit function we're actually getting our title in with an underscore now so we want to convert that underscore to a space when we go to render the actual string so we say if strings contains and we're going to say if this contains an underscore and let's fix contains then we want to replace the underscore with a space so that should work for us now so now when we render our test element to the screen we will be rendering it without underscores would be rendered with spaces so it'll look a little neater to the user and we need to do the same for our edit which is down here now this is of course going to cause a problem which we will fix in a moment but first I want to show you why we're getting a problem and then we also want to do the same for our save so in here we want to say title equals strings dot replace title and we want to replace all the spaces with underscores in this alright so now let's go ahead and run this and take a look at our application all right so we need to create a user now so our user will be uppercase tensor and now we've logged in so let's create a page and let's just call this page hello world not saying this is our hello world page and let's create the page and as you can see we've got a page called hello world in the URL it has an underscore but as it's being rendered it's being rendered with a space even up in our navbar it's being rendered with the space however if we click the Edit URL this is going to cause an error as you can see 404 page not found now what's happening is we're sending it in with a space here because we're rendering it with a space and the way that we're rendering this link in our template is by actually sending in the title so if we go into our template here so our test template as you can see here it says edit and then dot title we're also going to have a problem with our edit title here as you can see we have a save and then we have dot title as well so the way that we fix this is we add functionality to our templates and the way that we add functionality to our templates is we have to go back into our main dot go file and we have to basically create that functionality so if we go to our render function here we can create that functionality so we can create a small function that allows us to basically take a piece of data and manipulate it so in this case we want to manipulate the piece of data so that when it's in a URL setting it has the underscores what we're going to do is we're going to create a variable called func map we're going to set that equal to template dot func map now this is of course something that's built into our function our template rather and this is allowing us to create a string that we can parse inside of the template itself and when it gets parsed by the go parser it will read it as a function so what we will say is ok if we want to create a function called URL eyes and that's what we're going to create we want to bind it to this function that takes in a string and outputs a string and returns strings dot replace and then we put in s we replace the spaces with underscores and of course we do it like we were doing before of course we need a , here so now to actually put this into our templates we need to say okay template new and we're gonna pass name in here and then we want to add funks and then we're gonna pass it in funk map and then we're gonna say parse Grom and what this will do is it will create a new template so a static template every single time with the functionality of this parse map here with this you know small anonymous function connected to it inside of our glob so now let's rerun our program before we've actually edited our templates and so if I reload it's going to ask me to relog in so I've logged in and if we search out hello world it should find hello world which is this page that we've created before so now if we hit edit it will still have a problem let's now edit the template itself so if we go in here we can say okay title and then we can pipe in your allies which is the name of the function that we created before so let's save this and let's reload this alright so now we're back at our test and we had we didn't have to rerun run our our actual project because we're just adding the templates so now if we actually inspect the link as you can see down here it says edit and then hello underscore world so our your allies function is now working as intended and if we click Edit instead of getting an error it should lend us to the edit page so as you can see we're now on the edit page now of course we're going to get an error if we click Save and I'll show you why so we'll inspect to save real quick and as you can see here save is going to hello space world with this percent 20 in between it percent 20 is a space so if we hit save this will save it as hello space world which is what we don't want so let's go back luckily our reg X is actually protecting things so when it's throwing this 404 page not found the reason why is because hello world with a space and it does not actually fulfill our reg X inside of our closure it doesn't actually get saved to our database and it doesn't actually create a file called hello space world so let's go edit the template save and fix that so in here we have our save so all we have to do is add a pipe and then we type in your are allies and this will of course fix everything so now if we reload this and we hit save it works correctly alright guys so I hope you enjoyed this tutorial if you did feel free to subscribe and like and of course feel free to comment if you have any questions and by all means if you dislike the tutorial then feel free to download it as much as you want so before I go I just wanted to mention that we will be having a poll for the next programming language that we are going to go over in our video tutorials I'd like everybody to participate if they can I'll probably have an official announcement video at some point when I actually do create the poll that way people on YouTube don't get left out and you don't have to follow me on Twitter either alright guys so I hope you have a good night

Original Description

In this tutorial, we add multi-word titles to our program and also add a custom function to the template language in 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-16 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 · 29 of 60

1 NodeJs, Text editors and IDEs
NodeJs, Text editors and IDEs
Tensor Programming
2 Vanilla JS todo App
Vanilla JS todo App
Tensor Programming
3 Elm Tutorial part 1
Elm Tutorial part 1
Tensor Programming
4 Elm Lang Tutorial, Part 2
Elm Lang Tutorial, Part 2
Tensor Programming
5 Elm Tutorial Part 3
Elm Tutorial Part 3
Tensor Programming
6 Elm Tutorial Part 4 -- Analog Clock App
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
7 Elm Tutorial part 5 -- Snake Game
Elm Tutorial part 5 -- Snake Game
Tensor Programming
8 Elm Tutorial part 6 -- Calculator
Elm Tutorial part 6 -- Calculator
Tensor Programming
9 Go Tutorial part 1 -- Hello World and Static File Server
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
10 Go Tutorial part 2 -- Web Crawler
Go Tutorial part 2 -- Web Crawler
Tensor Programming
11 Go Tutorial Part 3 (Web App part 1)
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
12 Go tutorial Part 4 (Web tutorial part 2) - Using templates
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
13 Go tutorial part 5 (web app part 3)
Go tutorial part 5 (web app part 3)
Tensor Programming
14 Go tutorial part 6 (webapp part 4)
Go tutorial part 6 (webapp part 4)
Tensor Programming
15 Go tutorial part 7 (web app part 5)
Go tutorial part 7 (web app part 5)
Tensor Programming
16 Go tutorial part 8 (Web app part 6)
Go tutorial part 8 (Web app part 6)
Tensor Programming
17 Go tutorial Part 9 (web tutorial part 7)
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
18 Go tutorial Part 10 (web app part 8)
Go tutorial Part 10 (web app part 8)
Tensor Programming
19 Go tutorial Part 11 (Web app Part 9)
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
20 Go Tutorial Part 12 (Web app Part 10)
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
21 Go Tutorial Part 13 (Web app Part 11)
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
22 Looking at Elm 0.18
Looking at Elm 0.18
Tensor Programming
23 Go tutorial Part 14 (Web tutorial part 12)
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
24 Go tutorial Part 15 (Web tutorial part 13)
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
25 Go tutorial part 16 (web app part 14)
Go tutorial part 16 (web app part 14)
Tensor Programming
26 Elm Tutorial Part 7 (SPA part 1)
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
27 Elm Tutorial Part 8 (SPA Part 2)
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
28 Electron Elm Tutorial
Electron Elm Tutorial
Tensor Programming
Go tutorial part 17 (web app part 15)
Go tutorial part 17 (web app part 15)
Tensor Programming
30 Up and Coming Programming Languages and Technologies for 2017
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
31 elixir tutorial part 1
elixir tutorial part 1
Tensor Programming
32 elixir tutorial part 2
elixir tutorial part 2
Tensor Programming
33 Elixir tutorial Part 3 (GenServer and Supervisor)
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
34 Elixir Tutorial Part 4 (GenStage)
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
35 Elixir Tutorial Part 5 (Plug and Cowboy)
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
36 Phoenix Framework Tutorial Part 1 (elixir part 6)
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
37 Phoenix Framework Tutorial Part 2  (elixir part 7)
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
38 Phoenix Framework Tutorial Part 3 (elixir part 8)
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
39 A Intro to Clojure and Clojure Syntax
A Intro to Clojure and Clojure Syntax
Tensor Programming
40 An Update about the channel
An Update about the channel
Tensor Programming
41 Intro to Rustlang (Setup and Primitives)
Intro to Rustlang (Setup and Primitives)
Tensor Programming
42 Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
43 Intro to Rustlang (Ownership and Borrowing)
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
44 Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
45 Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
46 Intro to RustLang (Enums and Options)
Intro to RustLang (Enums and Options)
Tensor Programming
47 Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
48 Rustlang Project: Snake Game
Rustlang Project: Snake Game
Tensor Programming
49 Intro to Rustlang (Traits and Generic Types)
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
50 Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
51 Intro to Rust-lang (Modules and Lifetimes)
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
52 Intro to Rust-lang (Macros and Metaprogramming)
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
53 Intro to Rust-lang (Error Handling)
Intro to Rust-lang (Error Handling)
Tensor Programming
54 Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
55 Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
56 Rustlang Project: Port Sniffer CLI
Rustlang Project: Port Sniffer CLI
Tensor Programming
57 Rustlang Project: Chat Application
Rustlang Project: Chat Application
Tensor Programming
58 Rustlang Project: CLI Toy Blockchain
Rustlang Project: CLI Toy Blockchain
Tensor Programming
59 Intro to Rust-lang (Setting up a Development Environment)
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
60 Intro to Rust-lang (Building a Web API with Iron)
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming

This tutorial teaches how to add multi-word titles and customize functions in Go templates, covering topics like regular expressions, URL encoding, and template functionality. By following the steps, viewers can improve their skills in crafting and using prompts in Go applications.

Key Takeaways
  1. Delete the pkg folder and rebuild projects from scratch when updating to Go 1.8
  2. Modify regular expressions to allow for underscores in URLs
  3. Use double quotes for regex patterns in Go
  4. Replace spaces with underscores using string::Replace function
  5. Create a small function in the main .go file to add functionality to templates
  6. Use template.FuncMap to create a string that can be parsed inside the template
  7. Edit the template itself to use the new function
  8. Inspect save button to see why it's causing an error
  9. Edit template to add a pipe and URL encode save functionality
💡 Using template.FuncMap to create parseable strings inside templates allows for more flexible and customizable functionality in Go applications.

Related Reads

Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →