 Saya di Kai's Camp Kita kisah? Bagaimana kita kisah? Dalam latihan saya, saya berada di Backend Service Dan saya cuba menghubungi dasar sebelumnya Tapi baru-baru, klien saya kata, saya mahu mempunyai pakaian Jadi saya menghubungi diri saya pada HTML Jadi dengan itu, saya perlu menggunakan tutorial Di mana Kai datang Itu adalah tutorial untuk menggunakan pakaian Jadi dengan pakaian Saya akan membuatnya kawan untuk orang di belakang Dengan pakaian Ini adalah pakaian yang sangat penting Dan anda dapat menggunakan pakaian Jadi pakaian ini mempunyai pakaian yang anda mahu menggunakan Jadi jika saya mempunyai pakaian dengan dunia Ia akan berkata, dunia Saya membuat pakaian Saya membuat pakaian Jika saya mempunyai dunia Boleh saya menggunakan pakaian yang saya mahu membuat Saya akan menggunakan pakaian Saya akan membuat pakaian Berlambut Saya akan menggunakan pakaian Bagi saya membuat pakaian Baiklah Saya boleh membuat pakaian yang betul Jadi pakaian saya kata Pass, hello, salutations, there's this minor stuff over here which I didn't explain back word. Dot capital name, dot capital age, and you must use capital letters when you use a struct. So I'm going to pass in a struct here. Salutations starting with capital S, that's just the way it works. With a capital name and capital age. So if you think you're sure, you'll say hello Mr. Tan. And minus sign. Okay, let's run that. It says hello Mr. Tan. But if I remove a little minus sign here. Look at it between the space of Mr. Tan and the space there. So the minus sign eats up white space. So tutorial number one, the minus sign at the back eats up white space at the back. Minus sign in front eats up white space in the front. It's a white space eater. But those people who are Java, JS, JavaScript people, they don't like to see capital letters. Let me load it up again to be back row friendly. As you can see, good. So let's try replacing this L with a capital S. Okay, sensitive languages, you'll blow up. It should blow up. No value Mr. Tan, because couldn't find capital S. Salutations. Put it back there. Run that again. Now you have Mr. Tan again. Notice the minus sign at the back. You have won 233 apples. Where does the tree come from? The tree comes from here. Okay, if I replace this tree. And I run it again. And I run 4 apples. So far so good. What if I replace the 4? With this funny thing over here. You never seen that? I don't know. You think you're allowed? Let me give you a hint. Go stringify all the output. So when you stringify that, when you stringify that, it comes out like that. When you print line that, it shows you're at the 1, 2. But notice this is a map of string to interface. An interface in Go means anything under the 5. I can put in this really, really, really. Okay, 2 plus 3. I guess everybody will know. Replaces and extremifies to 5. Let's validate that. What did I do? Ah, yes. 1, 5 apples. So it stringifies. Now, here comes the really, really yaki, surprising, functional programming part. Those functional programming people can share with you. Let me explain this a little bit. This is a function that takes in a float32. That returns a float32. And that function does is just doubles it. And I put in the value of 2.3. So you should be able to... If you stringify 2.3 times 2 to the mathematics and you should get, let's validate that. 5, 4.6. Ya, okay. So that is an extremely powerful Go term. Do you know about that? No. That's really, really, really powerful. In my mind, when I first saw that. Let's get on with the talk. Okay. So you can have templates, your functions passed in. This is a standard Golang template function. Which is basically to print that function. Lowercase p, not the capital P. And it has one argument, two arguments, three arguments. That's a call with three arguments. I can issue my own function. So here I put in my own time function. And that time function is down here. Because I say it's over here. We turn time now. And in this format, the second time function is... It will be called an offset. My time offset function is this thing over here. So it's blow your mind. Simple yet complex. Let me blow it up. And talk piece by piece. What we are passing into the template. Is a map of string. To interface. Which means I give my function a name. That's a string. And the interface, means anything under the sub. So my name, time, is a function. And it takes no arguments. And returns a string. And what the function does, is just returns the current time. With the formatting. My name is offset. Returns integer 2. My name is t offset. And returns another funny function. Which is a time function that adds a certain duration. Which is the offset duration, 2 hours. So let's call it back out. And run this. Right now it's almost 9 o'clock. In 2 hours time it will be 10.24. If I change the data. In 1 hours time. It will be 9.35. So we can do really dynamic stuff. And you can call your own functions. You can supply your own functions. It has conditions. Conditional temperate. That make it echo friendly. So if. This value turn. Returns a positive value 1. Anything that's not 0 of blank. If it returns anything that's not 0 of blank. It will. Execute turning third. If not. It will say it goes straight. So let's pull that out. And run this. Let's predict what it will show. We get that black box out of the way. So if I have a blank. It says going straight. If I have a value 1. Anything that's not 0 of blank. It says going straight. If I have a value. It says turning right. Which is the value. If I put in a boolean. Which is true. You will say true. If I put in a value 0. It says going straight. So 0 and blank. It's equal to now. It says going straight. 0 is the same as 0. And blank. So if you have a stroke. Without. No. Without if. Alright. Let's move on. It's got iterations. Let's make that back world friendly. It's got iterations. So. The fun stuff. Starts here. Range. Dollar i dollar value. These are tempted variables. These are something you can find. It's a for loop. For stuff in data. Assign it to the index. And to the value. That's basically what it means. For the stuff in data. So. That's the index. That's the value. Dot. Also says the value. No data to show. Say no data. So that's the for loop. Let's execute that template. So. By execute that. Get that black stuff out of the slides. So I've got 4 items here. Y4 items. A, B, blank. It's a value. Black and zero are values. D. That's why there are 4 items. The value is. A, B, blank and B. The dot also evaluates to A, B, blank. On ranges. So far baby steps. Nothing challenging anymore. Go has this really, really, really weird thing. Really weird. And I don't know how to use this thing. Don't really know how to use this thing. But it says. Here's my range again. Range over something. Range over. This thing. Which is a queue, which is send ins. So what does send ins do? Send ins makes a channel. And it sends 5 integers. Once a second. It sends 5 integers. Once a second. So if you're doing network programming stuff. This will receive from the network. And. Print out stuff from the network. Cool stuff. I don't know how to use it. But let's see it in action. 2, 3, 4, 5. Exit. It exits. It knows how to exit. Because of this closed statement over here. It only knows how to exit. Because I closed the channel. If I don't close the channel. Let's see what happened. 1, 2, 3, 4. Boom. What did they boom? I don't know what to do. I'm stuck. But as I said, this is a really esoteric thing. I look up the docs. I really don't know how to use it. But I put it there for complete lesson. Now I have to do the really fun stuff. Template composition. Functional GIMR. So back row friendly. So I have a template name. I name it just for convenience. You don't need to have names. You can put in the blank string here. But this template is called AD. I'm defining template A here. I'm not executing it. I'm defining it as a declaration. Template A is defined as I and A for A value. And the definition ends with the end statement. Defining template B. You now know what the dashes do. It eats up white space. I and B for B value. And B to N. I'm calling the template. I'm including the template here. So template A, A-stuff, template B, B-stuff. So here I'm defining templates. Here I'm calling templates. When I execute that template, let's see what happens. It's pretty simple. I'm A for Apple. Why? Because A, A-stuff A-stuff is for A value. And B-stuff B-stuff It's actually B-stuff over there. B-value boy and B2 back. So that's how we use templates. I got stuck on this for the longest time. Can I code I thought the keyword I did was something like include. You can use include, but I just use the template It's a different file. Okay. How about A and closing B? A bit punky here. It gets more punky. Back row friendly. So I'm defining A here. I'm A for A value. I'm still in template A. But I'm executing template B now. So B is inside template A. A is the master, B is the slave. And B is B-stuff. And now I define it post definition I am B for B value. And I call it once template A. Because the only one template A B is included in A. Let's execute that. So A for Apple is quite clear because A value A value is Apple. But it's here which is a map of string to interface B value. And B is the same to the template. I pass A to B-stuff and it works. I don't expect you to digest this code. It's all on GitHub. We can download it. But here is some stuff which I bet kind doesn't go far. Because it's so competitive. Let's say we have a form letter Back row friendly again. Let's say we have a form letter I have a body function. It says letterhead Dear body sincerely close. It's a form letter. It's a junk mail generator. It's a junk mail generator. So I must pass I can define good news. Good news say I'm pleased to inform you. I can define bad news. We are sorry to inform you. So that's the form letter. An output can be captured into a buffer. You don't have to write to send an output. You can capture it to a buffer. And this is the interesting part. I can define body function. I can define body function that takes nothing for its input. And outputs a string. And returns a buffer string. So this function basically what it does is output and sticks it into the body. So if I execute this I get good news. Dear whatever I'm pleased sincerely. If I change this data to bad news and run it again it says Dear we are sorry. So I'm taking the good news template and bad news template and sticking it in the layout. Imagine now that this is your HTML with your 5MB javascript header and your CSS and whatever in there. HTML header HTML footer and stuff you will try to insert in between. So that's what this layout function does. I stole this from the book. But I thought it was such a neat thing. Forget about that. It was such a neat thing. I said I should share with the Go community. So as you have seen in KaliStore you can actually have templates in the file system. Template A Template B Template B So these if I have these files in the file system it would actually be called template A.tpl because the name of the file is .tpl. If I don't want that .tpl I don't want to put .tpl in there. I want to define the name for it. That's basically what I'm trying to say in this slide. So template A follow by template B let's execute that. Before we execute that let's show the code. So the interesting stuff is here. Pass everything under the FS sub directory. Everything with the .tpl extension. So everything in the .tpl extension was what I just showed you. I'm going to execute it. You should see the expected result. As usual, I'm going to template B with my value with void et cetera. Because I executed as template B. If I change it to template A you will blow up. Why? Let's see why. I'm asking template A to execute with the map B value and replace it with the .tpl what do you think will happen? Always replace templates with .tpl because .tpl is not defined. You actually have to put in some data in there. You have to execute it with the map. So if you execute it with M you get the really weird stuff at the back. The whole map. Templates are very rich in book. You can apply all sorts of functional programming tricks. And you can do the same thing with HTML. Exactly the same, I won't go through it. But I will show you the end result. I have my render template which is a map of string to interface. My navigation, my main body, my footer. Out renders, HTML with style sheets, everything. My navigation, my main body So that's your layout. And if I change the data main body. So all this code is available from Github Github-Syurian present Go templates. I hope you enjoyed that simple forum. Interface stringifies any object that stick in. So just stringify it. When you stringify it, zero evaluates the false. Blank evaluates the false. Everything else evaluates the false. Everything else evaluates the false. Just like HTML, just like ERB. Will you work with that in your views? Ah. Don't put business logic in views. But automatic contextual escaping. And must put card. Okay. It's just another function that you find. Did you jump real wide? Ya. But let's contextual whatever, whatever. Let me close this so I can see what I'm doing. Contextual escaping. Script keys love this car style. Ya. It's an injection attack. So let me put in an injection attack right there. My injection attack. What do you think happened? That's fine. HTML templates. Will you escape? I'm not sure. It will be escaped. See? My M% less than script. M% greater than escape. No. So it's idiot proof. Both templates are idiot proof. Unless you say it's HTML you have to explicitly tell it to escape, to not escape. Oh ya, okay. Ya, you have to explicitly tell it not to escape. Otherwise, it will protect you. It protects you from dung-dung, dung-dung. Sorry? HTML. HTML dot HTML. Kapital HTML. HTML. HTML is a package. Dot Kapital HTML is Tricked as unescape HTML. So if I say I didn't import HTML so it won't work. But if I imported it, it would show the script and pop up. So simple tutorial. I thought it would be useful. You're wrong. Don't say M4. M4 is nasty. The Ruby templating systems are really nice. But slow. I didn't say not. I didn't say fast. I said nice. What does Ruby do? It doesn't have this correctly double-curring nonsense. It's not a program. It's not just like the most popular way of doing it. I want you to say that the developers or people who are not don't know go-lay at all and they understood the template. Ruby doesn't give you the start. It reads like English. Oh, until you read like English. I mean, I'm a Ruby guy. I came from the Ruby world and I'm loyal to Ruby. But go is better. Go is better. So I've moved over to the go-side for a few reasons. One reason is it doesn't need resources. I wrote servers in Ruby before. Servers in go. They can stay up for months and they don't need resources. 10 big mistakes. I did this. Oh, yeah. Boom. I don't want people thinking that they can do an incredible robust code. It's the presentation over today. It's a lot more important. We're just experts. I like mushrooms. I love mushrooms. Okay. Not the smoking guy. Sorry, we don't have a problem. What was the details? Nah, it's funny stuff. See you. It's the last minute for this year.