-2

Recently i started one school project, i need to make a web forum using only standard Go library. And the main obstacle i have is i don't know how to properly make routing. For example:

router.HandleFunc("/threads", threadsHandler)
router.HandleFunc("/threads/", postsHandler)

It is okay if i have only 2 routes. But i want to be able to handle more complex routes, like:

"/threads/{thread_name}/posts"

How do i do that without using Gorilla/Mux?

a b
  • 947
  • 1
  • 6
  • 7
  • 1
    Write the dispatch logic in application your code. In the handler for `/threads/`, examine the request URL path and dispatch based on that. – Charlie Tumahai Apr 19 '20 at 17:20
  • 1
    You can check how routing is done here https://github.com/aquasecurity/lmdrouter/blob/e942d976aa03f771fbb3711563481a30d2126d13/lmdrouter.go#L176. You can build something similar for your requirement. – praveent Apr 20 '20 at 02:41
  • There is nothing wrong with using some "programming". Not all things must be done by calling into some package. – Volker Apr 20 '20 at 06:01

1 Answers1

-1

Simply you can use HTTP package to handle this case. So this package provides HTTP client and server implementations. So I think this will help you.

func main() {
   http.HandleFunc("/threads/{thread_name}/posts",threadsHandler)   
   log.Fatal(http.ListenAndServe(":8080", nil))
 } 

And you can go to the https://golang.org/pkg/net/http this link to further details

David Buck
  • 3,752
  • 35
  • 31
  • 35