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.9k Golang : Accurate and reliable decimal calculations
+22.3k Generate checksum for a file in Go
+9.6k Golang : Get current, epoch time and display by year, month and day
+36.1k Golang : Convert date or time stamp from string to time.Time type
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+10.8k How to test Facebook App on localhost ?
+7.6k Golang : Getting Echo framework StartAutoTLS to work
+21.7k Golang : Join arrays or slices example
+26k Golang : Convert(cast) string to uint8 type and back to string
+10k Golang : Random Rune generator
+4.8k Golang : Constant and variable names in native language
+21.9k Golang : Securing password with salt