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.5k Golang : Generate human readable password
+11.9k Golang : Simple client-server HMAC authentication without SSL example
+14.1k Golang : How to determine if user agent is a mobile device example
+13.4k Golang : Check if an integer is negative or positive
+8.1k Golang: Prevent over writing file with md5 hash
+9.2k Golang : Extract or copy items from map based on value
+14.3k Golang : Convert(cast) int to float example
+7.1k Golang : Individual and total number of words counter example
+8.9k Golang : does not implement flag.Value (missing Set method)
+34.7k Golang : Upload and download file to/from AWS S3
+6.9k Golang : Validate credit card example
+16.2k Golang : Delete files by extension