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
+30.6k Golang : Generate random string
+6.2k Golang : Scan forex opportunities by Bollinger bands
+17.7k Golang : Parse date string and convert to dd-mm-yyyy format
+9.7k Golang : Read file with ioutil
+12.9k Golang : Listen and Serve on sub domain example
+14.1k Golang : convert rune to unicode hexadecimal value and back to rune character
+8.7k Golang : Convert(cast) []byte to io.Reader type
+14.4k Golang : Get uploaded file name or access uploaded files
+5.5k Golang : Display advertisement images or strings on random order
+6.3k Golang & Javascript : How to save cropped image to file on server
+5.3k Python : Convert(cast) string to bytes example
+8.9k Golang : Gorilla web tool kit schema example