DEV Community

Ega Prasetya
Ega Prasetya

Posted on

1 2

Golang Web - Routing Handle Func

In GoLang there are several ways you can do..

  • By using http.HandleFunc
  • Interface implemantations http.Handler in the struct, and then using in the function http.Handler()
  • Create multiplexer by using struct http.ServeMux
  • Etc.

But in the series, we just used http.HandleFunc, okay so lets do it.

http.HandleFunc()

as explained briefly in the previous chapter, the http.HandlerFunc function is used for route / endpoint registration and its handler.

  • First step, create main.go and import package net/http

Alt Text

  • Next, create function main() and add closure handlerIndex, and then used it closure as handler from new 2route registation, that is / and /index.

Alt Text

  • Still in function main(), add the new Route /data with handler is anonymous function.

Alt Text

  • Last step, run the server

Alt Text

  • So, Run & Test

Alt Text

Alt Text

Grats, hope your enjoy it.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️