일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- SWIFT
- GoCD
- openpyxl
- ssh
- appium
- nohup
- mysql
- postgresql
- ftp
- STF_PortForwarding
- appium server
- centos
- sshpass
- STF
- 실행권한
- insert
- kitura
- port forwarding
- postgres
- PYTHON
- rethinkdb
- Jupyter
- Jupyter Notebook
- perfect
- Materials
- ubuntu
- nmap
- nGrinder
- create table
- 28015
- Today
- Total
don't stop believing
Parameters in path 본문
[https://github.com/gin-gonic/gin#parameters-in-path]
http path 안에 parameter를 넣는 경우입니다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556package mainimport ("strings""github.com/gin-gonic/gin")func setupRouter() *gin.Engine {// Disable Console Color// gin.DisableConsoleColor()r := gin.Default()r.GET("/someGet", someMethod)r.POST("/somePost", someMethod)r.PUT("/somePut", someMethod)r.DELETE("/someDelete", someMethod)r.PATCH("/somePatch", someMethod)r.HEAD("/someHead", someMethod)r.OPTIONS("/someOptins", someMethod)r.GET("/user/:name", func(c *gin.Context) {name := c.Param("name")message := name + " is very handsome!"c.JSON(200, gin.H{"message": message})})r.GET("/user/:name/age/:old", func(c *gin.Context) {name := c.Param("name")age := c.Param("old")message := name + " is " + age + " years old."c.JSON(200, gin.H{"message": message})})r.GET("/colour/:colour/*fruits", func(c *gin.Context) {color := c.Param("colour")fruits := c.Param("fruits")fruitArray := strings.Split(fruits, "/")// remove the first element in fruit slice.fruitArray = append(fruitArray[:0], fruitArray[1:]...)c.JSON(200, gin.H{"color": color, "fruits": fruitArray})})return r}func main() {r := setupRouter()// Listen and Server in 0.0.0.0:8080r.Run(":8080")}func someMethod(c *gin.Context) {httpMethod := c.Request.Methodc.JSON(200, gin.H{"status": "good", "sending": httpMethod})}
우선 확인할 것은 handler 함수를 지정해 별도 정의할 수도 있고 익명 함수로 바로 사용할 수도 있다는 것입니다.
이번에는 익명 함수로 사용했습니다.
확인할 함수는 아래 세 개입니다.
r.GET("/user/:name", func(c *gin.Context) {
name := c.Param("name")
message := name + " is very handsome!"
c.JSON(200, gin.H{"message": message})
})
도메인과 포트 뒤에 /user/:name 이 오게 됩니다. :name에 해당하는 경로를 parameter로 사용할 수 있습니다.
r.GET("/user/:name/age/:old", func(c *gin.Context) {
name := c.Param("name")
age := c.Param("old")
message := name + " is " + age + " years old."
c.JSON(200, gin.H{"message": message})
})
이번에는 path 안에 parameter가 두 개가 있습니다. :name과 :old 입니다. 이렇게 뒤에 계속 붙일 수 있습니다.
r.GET("/colour/:colour/*fruits", func(c *gin.Context) {
color := c.Param("colour")
fruits := c.Param("fruits")
fruitArray := strings.Split(fruits, "/")
fruitArray = append(fruitArray[:0], fruitArray[1:]...)
c.JSON(200, gin.H{"color": color, "fruits": fruitArray})
})
이번에는 특정경로 하위의 내용을 통으로 묶어서 parameter로 받는 방법입니다.
/colour/:colour/*fruits 라고 했을 때 *fruits에 해당하는 하위 경로는 모두 하나의 parameter로 밭게 됩니다.
만약 /colour/red/apple/cherry/guava 일 경우 *fruits에 해당하는 "/apple/cherry/guava"를 전달받게 됩니다.
여기에서 string의 특정 character를 기준으로 split 하게 했습니다. 그럼 slice형태로 전달됩니다.
그리고 slice의 첫번째 element를 제거했습니다.
fruitArray := strings.Split(fruits, "/")
fruitArray = append(fruitArray[:0], fruitArray[1:]...)
이제 서버를 실행하고 첫 번째 경로부터 확인해 보겠습니다.
1234567891011121314151617181920$ go run main.go[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.- using env: export GIN_MODE=release- using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /someGet --> main.someMethod (3 handlers)[GIN-debug] POST /somePost --> main.someMethod (3 handlers)[GIN-debug] PUT /somePut --> main.someMethod (3 handlers)[GIN-debug] DELETE /someDelete --> main.someMethod (3 handlers)[GIN-debug] PATCH /somePatch --> main.someMethod (3 handlers)[GIN-debug] HEAD /someHead --> main.someMethod (3 handlers)[GIN-debug] OPTIONS /someOptins --> main.someMethod (3 handlers)[GIN-debug] GET /user/:name --> main.setupRouter.func1 (3 handlers)[GIN-debug] GET /user/:name/age/:old --> main.setupRouter.func2 (3 handlers)[GIN-debug] GET /colour/:colour/*fruits --> main.setupRouter.func3 (3 handlers)[GIN-debug] Listening and serving HTTP on :8080
첫 번째 /user/:name 입니다.
두 번째 /user/:name/age/:old 입니다.
마지막 /colour/:colour/*fruits 입니다.
여기까지 path안에 parameter를 넣고 확인하는 방법이었습니다.
'Golang > Gin' 카테고리의 다른 글
Another example: query + post form (0) | 2019.02.15 |
---|---|
Multipart/Urlencoded Form (1) | 2019.02.14 |
Querystring parameters (0) | 2019.02.14 |
Using GET, POST, PUT, PATCH, DELETE and OPTIONS (0) | 2019.02.13 |
Gin 설치와 기본 example 확인해 보기 (0) | 2019.02.13 |