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
+15.4k Golang : File path independent of Operating System
+6.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+7.3k Useful methods to access blocked websites
+10.5k Golang : Change date format to yyyy-mm-dd
+13.2k Golang : How to shuffle elements in array or slice?
+10.6k Golang : Calculations using complex numbers example
+7.6k Golang : Executing and evaluating nested loop in html template
+17.8k Golang : Fix cannot download, $GOPATH not set error
+16.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+29.5k Golang : bufio.NewReader.ReadLine to read file line by line
+9.3k Golang : Allow Cross-Origin Resource Sharing request
+12.9k Golang : Get uploaded file name or access uploaded files