Go tutorial part 16 (web app part 14)
Skills:
Tool Use & Function Calling80%Prompt Craft70%LLM Foundations60%Prompt Systems Engineering50%
Key Takeaways
The video demonstrates how to implement a search handler in a web application using Go, and add consistency to page titles. It covers the use of HTTP handlers, database queries, and template engineering.
Full Transcript
hi guys welcome to go tutorial part 16 my name is tensor from the tensor programming blog and today we are going to implement a search Handler into our web application so we want to be able to let the user click into a search box type in the name of a page and then search for that page so this is something that we're going to add to our templates we're gonna put it inside of our nav bar so to get started we're going to just map out where we want our HTTP handler to be so we're gonna create a handle func and we're just gonna say search so this is just going to be a post so we'll just say okay search and we'll say check UUID and our function name is going to be search so right now as you can see it's throwing an error because it doesn't exist so let's create that function so let's go right here and our search is going to take in an HTTP response writer so it's going to take in the same form as a normal Handler there we go we've got our HTTP response writer and our HTTP request and the first thing we want to do is get the form value so our form value we're gonna call it s value we're gonna set that equal to our our top form value and our form value is just going to be search so this will bring in a string and then we want to create a function that will go into our database and actually pull out the page so we're gonna say if and we're gonna create a function called page exists and we're gonna pass in s value to say if it is true so if it comes back with a boolean of true then we are going to redirect to the page that we've brought out so we're going to redirect there by just concatenated the s value onto the page so we're gonna say test and then we're going to concatenate it with s value we're going to redirect there with a 302 we're also going to throw in a return statement so that we don't get any header errors finally if we do not find a value in here so we're gonna call our render function we're going to render a template called search and then we are going to input a new page variable in here with a title and that title will be equal to our s value so that's pretty simple as you can see this is a handler for the most part except for the bottom part which renders a search function so now we need to create this page exists function inside of our data that go file all right so our function is called page exists and it's just going to input a title string and we're going to throw back a boolean and an error as with all of our database functions we're going to start by opening our database and then we're going to throw out our different to create two variables so our one called PT which stands for page title and it's going to be a type string now one called PB which is called page body and it's a slice of bytes and then we're going to make our query as you can see here we're going to select the title and body from our pages table where the title is equal to the title that we're bringing in through our function here and then we're going to order it by timestamp and make sure that we only limit it to one we're gonna run some standard error testing here so if the air is not equal to no we're gonna throw back false for our boolean and the error then we are going to iterate through our page even though we're only bringing back one page we need to iterate through it so Q dot next and then we're just going to Q dot scan and we're gonna input our the pointer for PT and the pointer for PB this will allow us to read the variables and then we're just going to check and see if PT e does not equal to an empty string and PB is not equal to nil then we want to return true and nil toenails our error true is our boolean otherwise we're just going to return false and now so basically what this function is doing is we are going to take our title and if we get a query back we're going to run those values that we brought back from the parry and make sure both of them are populated with something and if they are then we're gonna send back true if they aren't we send back false and if we get an error we send back false in that error so we're not going to deal with the error in this if statement so we're just saying if B which in this case I just like to use B because it stands for boolean in my mind you can use whatever you want if you if it makes it more clear you know we get our value coming back as true then we go to that page if it comes back as false then we render our search page so now we need to create our search template so here is our search template as you can see it's very simple we're just defining a template called search and we're importing our header and our nav bar now we'll get to that in a moment and we're going to import our footer we're just creating a Jumbotron here so a div and then we're using an h3 and we're just saying title does not exist and then we have a link that says click here to create a new page and if you click on the new page it'll go to our create HTTP area next because of our navbar here we want to actually take our navbar out of our other templates and make its own template so here we go here's a navbar dot HTML file and as you can see in here we have our navbar so we've changed it though so what we're doing here is we have a nav bar and the nav bar brings in the page title so the page slider will be in the corner and then we have a link to our home so this will bring us back to our example then we have a link to our create page so this will allow people to create a page a link to upload and this is our upload and then most importantly here is our form and this is our form for our search post so as you can see here it says method of post action search and inside of this form we have a input box where we can just type in the page name and then we have a button where you can click Submit and all this will be inside of the navbar so it'll look kind of nice you know we're just using bootstrap to do that however because of the way that we're bringing in our title here if we do not have a title this is actually going to not render correctly so what we need to do is we need to actually create multiple different nav bars in some of our other files so here's our create file and you can see we used to have our navbar here I just removed it so this is to create a page now the thing is when we're in this create fire we do not have an actual page name so if we just bring in our navbar as we have it here it will actually not render so let's copy all this and go into our create paste it in and then we want to change this title and we just want to manually enter create and we want to change the name of the navbar so we're just going to call this navbar create and we need to of course change it up here so this will call on the correct template when we go to the create page and everything will be pretty consistent except for this will change this is something that we can streamline later down the line but for now this is how we're going to do it so we also need to do this for our internal page as you can see here we have navbar internal have our internal and the only difference is the example page text and the other place that we need to put it is inside of our uploads here you go we have our navbar upload as you can see it says title we just changed this to upload let's just say upload a file so this will all work out correctly and it will actually render so finally the two cases where we are going to import our navbar dot HTML nav bar is in edit so as you can see here we're just importing that far and we've removed the block navbar that we down we head down here and in tests we're importing navbar again and we remove the block navbar that we had down here so now let's run this all right so let's sign up let's create a new user we're just gonna call it tensor and let's just say Jackie Dao and let's make these upper cases and our email will be test add test com password okay so there's our passwords let's just log in and there we go so here we are welcome and as you can see here is their nav bar so let's search out a page we know that we have two pages thus far one is called test with the lowercase T and there you go we went directly to our test page we can also go directly to our edit page within the Lark see if we put an uppercase T and the reason that worked is because of something else I did so let's actually go back and delete our database so we're gonna log out and we're going to delete our database and I'm going to show you a potential problem that we will run into here and then how we can correct it now we're removing our database that this is something you should probably do every single time you change your file or your program is remove the database so that you can test everything so now if we rerun our program we now need to create a new user again so that's this time we'll create a new tensor name with an uppercase T and it'll be John Doe and the email will be the same and the password will be simple again and let's login and there we go so we can go to our create page or upload page and we can go back home if we look for test we will not find it if we look for edit we will not find it now the reason this is is because it's not looking for the actual test tests from the file that we have so what we can do is we can actually go and we can edit the file and then save it and now it's loaded into our database and we'll do the same for edit where we edit it save it now it's in our database and finally let's do it for view and this is the page that we created in our last tutorial and if we save it it will now be in our database so now if we type in test lower case you know bring us to test but if we type in upper case test it will say it doesn't exist so we want to have consistency here so we want basically for our title to be saved as an upper case in our file also we can't handle titles just yet that have spaces in them and this is something that we will deal with in another tutorial later so for now we just want to make sure that when we input our title into our database that title will then be uppercase and we can do this with a function called strings title and this is a built-in function in our strings library here so the way we want to do it is in our save function so when we save our title here we to save that title as an uppercase so we're gonna call those strings that title on our title here so now that we'll save it as uppercase then if we go down to our create page and we call strings dot title on our title here this will also make our title uppercase if we now go into our and delete our database again let's run our program if we reload it needs us to login so we need to sign up again alright so now we've logged in and if we type in test it will not find test so let's save our tests so test test and let's edit it and save it so now if we go to capital tstst and it will actually show up go to under lowercase it will not show up so we need to change this yet again so now to make everything even more consistent we just make sure that our s value will be capital so when it gets passed into our page exists we want to capitalize it so all we have to do here is all strings dot title on our s value as we pass it in here and let's do the same thing here that way our titles will always be capital again we delete our database re around our file where you sign up now if we save our pages again if we type in test with the capital T it'll find it we type in test with the lowercase T it'll find it if we type in edit with lowercase e it will find it and we type in edit with an uppercase e it will find it as well so now no matter what we type in it will find the correct page and we can do the same for view as well let's actually enter it into our database now if we look at it it will find it both as uppercase and lowercase so this helps make more consistency in our application and if we create a new title for example when we put in a lowercase letter say go go is a cool language and let's save this if we look for it as an uppercase go it will find it so let's take a quick look at our database as you can see here we have four pages test edit view and go all of them are uppercase so that successfully worked and they have all the bodies of text that you would expect inside of them alright guys so that's the end of this tutorial in the next tutorials we will be dealing with some of the things that we talked about for example we will add the ability to deal with multi-line titles by changing you know spaces into dashes instead so they can actually be rendered in HTTP also just keep in mind that when you create a search module in any kind of web app typically you want to do it with some kind of front-end technology so if I was to do this again usually I wouldn't use go and studied use something like Elm or react or closure script so that I could add a few more features that you can't get from the backend and all I'd have to do is expose parts of the database to the front-end components so that it would work correctly so I hope you enjoyed this tutorial if you did feel free to subscribe like the video and if you have any questions of course feel free to comment if you dislike the video then of course by all means feel free to download it as much as you want about it as much as you want in the comment section alright I hope you guys have a good night
Original Description
In this tutorial, we put a search handler into our web app and we also add more consistency to our page titles.
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 · 25 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
▶
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
📰
📰
📰
📰
Three ranking currencies and zero overlap: what 2025 Juejin AI roundups actually disagree about
Dev.to AI
How to Use Poe for Case Studies in 2026
Dev.to AI
10 Ways to Make Money Using AI Tools in 2026
Medium · AI
I got tired of switching AI SDKs every time I wanted to try a new model
Dev.to · zhongqiyue
🎓
Tutor Explanation
DeepCamp AI