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
+32.2k Golang : Convert an image file to []byte
+32.3k Golang : Convert []string to []byte examples
+18.6k Golang : Logging with logrus
+7.6k Golang : Process json data with Jason package
+9.5k Golang : Create unique title slugs example
+9.8k Golang : Load ASN1 encoded DSA public key PEM file example
+17.3k Golang : When to use init() function?
+23.2k Golang : simulate tail -f or read last line from log file example
+8.9k Golang : Executing and evaluating nested loop in html template
+24.7k Golang : How to validate URL the right way
+13k Golang : Convert IPv4 address to packed 32-bit binary format
+7.9k Golang : Example of how to detect which type of script a word belongs to