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
+34.9k Golang : Display float in 2 decimal points and rounding up or down
+7.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+33.4k Golang : Upload and download file to/from AWS S3
+12.8k Golang : syscall.Socket example
+4.1k Golang : Constant and variable names in native language
+4.2k Gogland : Datasource explorer
+19.7k Golang : Encrypt and decrypt data with TripleDES
+5k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.9k Golang : Serving HTTP and Websocket from different ports in a program example
+23.5k Golang : Get current file path of a file or executable
+8.6k Golang : Sort and reverse sort a slice of integers
+29.1k Golang : Interpolating or substituting variables in string examples