![]() ![]() None of these extra functions are a requirement of a view package, but I find them helpful when thinking about what my view is providing. Or I may want helpers for rendering HTML forms via Go structs using something like my form package. For instance, it is very common to want to render the current user’s avatar or email address in the navbar, but providing this data in every single template render can be tedious. In my applications I like to have some global helper functions available in all templates that aren’t provided by the html/template package. If logic starts to creep into your views you are likely doing something wrong, and it could become problematic down the road. The important thing to remember is that the code in a view should have as little logic going on as possible instead, it should focus entirely on displaying data. Our view could just as easily handle rendering XML, JSON, or other data types. If we are implementing an application using MVC, this will typically be server-rendered HTML that we want to return to the end user’s browser, but it doesn’t have to be. Given a specific page that we want to render, and data for that page, our view is responsible for generating the correct output. Views are responsible for rendering data. Specifically, most code will be classified as a model, view, or controller. Model-View-Controller, commonly referred to as MVC, is a pattern used to help organize and structure code. ![]() When you have no idea where to even start, you are left digging through hundreds of source files, but in a well structured program you can often guess where the code is even if you haven’t read the source code in the past. The reason for this is simple - if you need to jump in and start adding a new feature, fixing a bug, or doing anything else to a program it is much easier to do if you can guess where in the code you need to start looking. At first it is hard to tell what makes the difference, but over time you will quickly learn that the biggest factor is how well organized and structured the code is. Using MVC to Structure Go Web ApplicationsĪs you learn to program, you will eventually start to notice that some code bases are easy to navigate while others are confusing and hard to maintain or update. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |