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
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+10.3k Golang : Convert file content to Hex
+11.2k Golang : Fix - does not implement sort.Interface (missing Len method)
+18.3k Golang : Get path name to current directory or folder
+9.4k Golang : Web(Javascript) to server-side websocket example
+24.6k Golang : How to validate URL the right way
+9k Golang : Populate or initialize struct with values example
+11.7k Golang : Gorilla web tool kit secure cookie example
+5.3k JavaScript/JQuery : Redirect page examples
+7.6k Android Studio : AlertDialog to get user attention example
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+18.1k Golang : Check if a directory exist or not