> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/golang/go-gin-framework/get-query-parameter.md).

# Get Query Parameter

Untuk get query parameter bisa menggunakan method GetQuery() seperti *code* dibawah ini.

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", Get)
	r.Run(":5000")
}

func Get(ctx *gin.Context) {
	username, _ := ctx.GetQuery("username")
	password, _ := ctx.GetQuery("password")
	
	ctx.JSON(200, gin.H{
		"status": http.StatusOK,
		"data":   map[string]interface{}{
			"username": username,
			"password": password,
		},
	})
}

```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FWRsRP7G6pJkqIUm2H4eg%2Fquery.png?alt=media&amp;token=1855bc63-854f-4526-82f3-62dce2ed2052" alt=""><figcaption></figcaption></figure>

atau bisa juga menggunakan method ShouldBind() seperti *code* dibawah ini.

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", Get)
	r.Run(":5000")
}

type User struct {
	UserName string `form:"username"`
	Password string `form:"password"`
}
func Get(ctx *gin.Context) {
	var user User
	if err := ctx.ShouldBind(&user); err != nil {
		ctx.JSON(404, gin.H{
			"status":  http.StatusBadRequest,
			"message": err,
		})
	} else {
		ctx.JSON(200, gin.H{
			"status": http.StatusOK,
			"data":   user,
		})
	}
}

```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2FsS61Wq1R0X5g82ZuSCUu%2F1.png?alt=media&amp;token=d4a5eb56-dc41-4bf8-b2ce-996115cf076d" alt=""><figcaption></figcaption></figure>

Tempat penampung data selain struct juga bisa diganti dengan map string string.

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", Get)
	r.Run(":5000")
}

func Get(ctx *gin.Context) {
	user := map[string]string{}
	if err := ctx.ShouldBind(&user); err != nil {
		ctx.JSON(404, gin.H{
			"status":  http.StatusBadRequest,
			"message": err.Error(),
		})
	} else {
		ctx.JSON(200, gin.H{
			"status": http.StatusOK,
			"data":   user,
		})
	}
}
```
