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
+21.7k SSL : How to check if current certificate is sha1 or sha2
+44.9k Golang : Use wildcard patterns with filepath.Glob() example
+23.5k Golang : Read a file into an array or slice example
+6.5k Golang : Spell checking with ispell example
+15.8k Golang : Get current time from the Internet time server(ntp) example
+40.4k Golang : Convert to io.ReadSeeker type
+21.8k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+8.3k Golang: Prevent over writing file with md5 hash
+14.1k Javascript : Prompt confirmation before exit
+36.3k Golang : How to split or chunking a file to smaller pieces?
+17.4k Golang : Get future or past hours, minutes or seconds
+7.1k Golang : Squaring elements in array