Package main import ( "fmt" ) // Node is a node of a tree. What I find delightful is that Go’s standard library, also has packages that make testing HTTP servers as easy as building them. The net/http package of Go’s standard library makes it easy to attach HTTP handlers to any Go program. The following is an example implementation of a binary search tree, which is one of the most basic tree structures. Go’s a great hammer for a lot of nails, one of the areas where I find it fitting is building HTTP servers. The connection between nodes is called an edge (branch).Īdding a node to a tree is called insertion, and finding a node in a tree is called search. The elements that make up a tree are called nodes, the node with no parent at the top is called the root, and the node with no children at the bottom is called the leaf. Tree structure is a data structure suitable for representing hierarchical structures. It is a data structure that has the structure of a tree in graph theory. Since hierarchical structure goes well with tree structure, we will consider representing the root map as a tree structure. If we look at the URL, we can see that it is a hierarchical structure. HTTP routers can perform routing based on the data that URLs and response processing are mapped to (hereinafter referred to as route map). HTTP routers are sometimes called URL routers, or simply routers, but in this article, we will use the unified name HTTP router.Īn HTTP router is an application that ties the requested URL to the processing of the response, as shown in the following figure. Please have a look at the code and try to use it. The author has released an HTTP router package called bmf-san/goblin. Experience in using some kind of HTTP router.The following prerequisite knowledge is required to fully understand the contents of this article. I want to understand the implementation of HTTP routers that I usually use.I want to know how to extend the standard routing functions.I want to try to implement a simple algorithm with Golang.Want to deepen their understanding of Golang's standard packages.You have understood the syntax of Golang and want to try to build something.This article is intended to be useful for the following types of readers. The contents of each chapter that deviate from the main topic arddde described as columns. Chapter 2 Data Structure of HTTP Router.This article describes how to build your own HTTP router with the following structure. Be able to integrate the HTTP router into your own application after understanding all the implementation details.You will be able to implement an HTTP router that you want to use and that is easy to use.You can experience the fun of algorithms.The following are some of the benefits of creating your own HTTP router. Therefore, in actual application development, it is not uncommon to use a more sophisticated HTTP router. The standard package does not provide a lot of routing features.įor example, you cannot define routing for each HTTP method, you cannot use URLs as path parameters, and you cannot define routing using regular expressions. This article explains how to create your own HTTP router using the standard Golang package net/http. This article is a translation of - net/httpでつくるHTTPルーター自作入門
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |