regex - Preserve structs data for using it later -


i'm learning golang - coding small web blog, , writing router(i know there available few - gorilla mux, martini, etc).

i have simple struct

type routes struct {         method string     pattern string     handler handler } 

and regex matchers. can't understand how keep routes define in 1 place. using slice of structs idea(like []routes) keep them together?

p.s. meant personal understanding of how works together

your question not defined. told want implement routing functionality based on regular expressions, haven't told kind of tasks want achieve influence optimal or best data structure used.

you mentioned know lot of other implementations open source, maybe should check sources.

this answer might shows simple implementation of basic implementation how routing functionality using regular expressions.

if want able register regular expressions if matched request path , forward serving handler, yes, storing "rules" in []routes viable , simple option.

things keep in mind:

  • i compile regexp in advance , store result , not compile them each time awful waste of resources. routes struct should contain field of type *regexp.regexp instead of pattern (you can keep string pattern e.g. debugging purposes).

  • if routes struct grows bigger, consider storing pointers in slice , not struct values, e.g. []*routes because each time when loop on them (e.g. in each request see matches) or whenever create local variable 1 of routes, copy made values. copying large struct inefficient compared copying pointer fast.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -