# 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="/files/BKCRSGHCqlbYXStaNxeI" 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="/files/ow5cd8DGjPbtUN2NV3ja" 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,
		})
	}
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/go-gin-framework/get-query-parameter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
