DEV Community

Ega Prasetya
Ega Prasetya

Posted on

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.

Top comments (0)