SoFunction
Updated on 2025-03-03

Several ways to get user input

1. Define the structure

type UserInfo struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
    Add  string `json:"add"`
}

type ReturnData struct {
    Message string   `json:"message"`
    Status  string   `json:"status"`
    Data    UserInfo `json:"data"`
}

2. Query method for get request

func getBindUser(c *) {
    //Query is used to obtain the parameters entered by the user    name := ("name")
    //Use to convert the parameters entered by the user to numbers    age, _ := (("age"))
    //Use to obtain the parameters entered by the user and set the default value to "Shanghai"    add := ("add", "Shanghai")
    //After instantiating the structure, it is bound to the user input parameters    userinfo := UserInfo{
        Name: name,
        Age:  age,
        Add:  add,
    }
    //Structure instantiation and receive user input parameters for binding. There are two ways to instantiate. The following comments are calculated as one, while the uncommented one is another.    // returnData := make(map[string]interface{})
    returnData := ReturnData{}
     = "The loading configuration was successful!"
     = "200"
     = userinfo
    (, returnData)
    (returnData)
}

3. Post request

1. Method to obtain user input parameters and bind directly to json format

func returnDataBindUser(c *) {
    userInfo := UserInfo{}
    if err := (&userInfo); err != nil {
        (, {
            "messages": "Loading configuration failed!",
            "status":   500,
        })
        ("Passage failed....", ())
    } else {
        (, userInfo)
        ("Name: %s\nAge: %d\nAddress: %s\n", , , )
    }
}

2. The second method is to use formdata, and this function is obtained by from-data.

4. Set token to obtain

Here we set the token acquisition and use jwt method. Just use the user to enter any character to determine whether the token is used to test whether the security of this function is successful.

func glbloTokenIsNo(c *) {
    //This is a token for any character entered by the user. The script shelf will use jwt    token := ("token")
    //This section will determine whether it is an empty string. The string must be judged by an empty string, and the number can be judged by !=nil!    if token == "" {
        returnData := ReturnData{
            Message: "Failed to obtain token!",
            Status:  "401",
        }
        (, returnData)
    }
    (200, "Login successfully!")
}

5. Routing grouping

Note: At first I didn't understand why I wanted to group, but later I realized that the group owner is not used to manage routes. For example, your access url is very long, such as /api/user/addUser/ and /api/user/deleteUser, you can divide /api/app into a group.

apiGroup := ("/api/user")

When using it, it can be written as follows

//In this way, apiGroup means /api/user("/addUser", returnDataBindUser)

The above are the detailed contents of several ways golang can obtain user input. For more information about golang obtaining user input, please follow my other related articles!