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
+8k Golang : automatically figure out array length(size) with three dots
+14.4k Golang : invalid character ',' looking for beginning of value
+11.3k Golang : Search and extract certain XML data example
+25.6k PHP : Convert(cast) string to bigInt
+11.1k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+11.1k Golang : How to parse plain email text and process email header?
+29.4k Golang : Download file example
+5.3k Golang : How to get capacity of a slice or array?
+7k Golang : Handle Palindrome string with case sensitivity and unicode
+7.4k Golang : Implementing class(object-oriented programming style)
+12.1k Golang : How to get a user home directory path?
+12.2k Facebook PHP getUser() returns 0