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
+17.5k Golang : Login and logout a user after password verification and redirect example
+8.7k Golang : Simple histogram example
+6.8k Web : How to see your website from different countries?
+44.3k Golang : Use wildcard patterns with filepath.Glob() example
+15.2k Golang : Validate hostname
+18.9k Golang : Get RGBA values of each image pixel
+15.1k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+6.8k Nginx : How to block user agent ?
+16.1k Golang : Read integer from file into array
+9.1k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+5.1k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+13.5k Golang : How to check if a file is hidden?