Golang : Handle sub domain with Gin
Just a supplementary tutorial for the previous tutorial on how to deal with sub domain in Golang. In case you find it too troublesome like me to introduce NewServeMux just to handle sub domain, do check out Gin framework. Below is an example on how to handle sub domain with Gin framework in Golang.
This example is from previous tutorial on how to deal with query string with a minor modification.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/query", func(c *gin.Context) {
first := c.Request.URL.Query().Get("first")
c.String(http.StatusOK, "First is "+first+"\n")
second := c.Request.URL.Query().Get("second")
c.String(http.StatusOK, "Second is "+second+"\n")
})
router.Run("api.example.com:8080") // port 8080 is optional //<---- here!
}
See also : Golang : Listen and Serve on sub domain example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.3k Golang : Gorrila mux.Vars() function example
+6.4k Golang & Javascript : How to save cropped image to file on server
+8.8k Golang : Combine slices but preserve order example
+20.6k Golang : How to get own program name during runtime ?
+7.5k Golang : Calculate how many weeks left to go in a given year
+8.9k Golang : Gorilla web tool kit schema example
+12.2k Golang : Convert a rune to unicode style string \u
+12.6k Golang : How to check if a string starts or ends with certain characters or words?
+7.7k Golang : Dealing with struct's private part
+14.2k Golang : Compress and decompress file with compress/flate example
+8.3k Golang : Multiplexer with net/http and map
+14.2k Golang : Human readable time elapsed format such as 5 days ago