登录
首页 >  Golang >  Go问答

文件上传时遇到无效字符‘-’的错误

来源:stackoverflow

时间:2024-02-07 20:15:22 122浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《文件上传时遇到无效字符‘-’的错误》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

这是我使用 golang 的第二天。我遇到了这样的错误。

invalid character '-' in numeric literal

根据这个问题,我可以简单地在邮递员中使用“原始”正文格式,一切都很好,但是,我需要上传文件,而你不能从原始邮递员格式中执行此操作。我该如何解决这个问题?

这是我的控制器代码(由于这个原因我还没有实现文件上传原因)

func signup(w http.responsewriter, r *http.request) {

    var duser models.user
    var signupbody requests.signuprequest

    requestbody, err := io.readall(r.body)
    if err != nil {
        // handle error
        fmt.println(err)
        return
    }

    err = json.unmarshal(requestbody, &signupbody)
    if err != nil {
        // handle error
        fmt.println(err)
        return
    }

    // if err := json.newdecoder(r.body).decode(&signupbody); err != nil {
    //  fmt.println(err)
    //  helpers.resposeformater(w, err, "something went wrong", 500)
    //  return
    // }

    defer r.body.close()

    switch {
        case signupbody.email == "":
            helpers.resposeformater(w, "email field cannot be empty", "email field cannot be empty", 422)
            return
        case signupbody.password == "":
            helpers.resposeformater(w, "password field cannot be empty", "password field cannot be empty", 422)
            return
        case signupbody.confirmpassword == "":
            helpers.resposeformater(w, "confirm field cannot be empty", "confirm field cannot be empty", 422)
            return
        case signupbody.firstname == "":
            helpers.resposeformater(w, "first name field cannot be empty", "firstname field cannot be empty", 422)
            return
        case signupbody.lastname == "":
            helpers.resposeformater(w, "last name field cannot be empty", "lastname field cannot be empty", 422)
            return

    }

    //validate email
    if err := configs.db.first(&duser, "email=?", signupbody.email).error; err == nil {
        helpers.resposeformater(w, "user with this email already exists", "user with this email already exists", 422)
        return
    }

    //validate password
    if signupbody.password != signupbody.confirmpassword {
            helpers.resposeformater(w, "password doesn't match", "password doesn't match", 422)
            return
    }

    hashpass, err := helpers.hashpassword(signupbody.password);  

    if err != nil {
        helpers.resposeformater(w, err, "something went wrong", 500)
        return
    }

    user := &models.user{
        firstname: signupbody.firstname,
        lastname: signupbody.lastname,
        email: signupbody.email,
        password: hashpass,
        picture: "",
    }

    if err := configs.db.create(&user).error; err != nil {
        helpers.resposeformater(w, err, "something went wrong 45", 500)
        return  
    }

    helpers.resposeformater(w, user, "success", 201)
}

到目前为止,我只尝试从 unmarshal 切换到 decode

我完全知道这没什么用处,所以如果您需要任何东西,请告诉我。带有参考的答案将非常慷慨。

编辑:

注册请求

type signuprequest struct {
    email           string `json:"email"`
    firstname       string `json:"firstname"`
    lastname        string `json:"lastname"`
    password        string `json:"password"`
    confirmpassword string `json:"cpassword"`
}

原始身体

{
    "firstname": "Melly",
    "lastname": "Melly",
    "email": "[email protected]",
    "password": "thepassword",
    "cpassword": "thepassword",
    "picture": "no"
}

正确答案


这就像魅力:

package main

import (
    "encoding/json"
    "fmt"
)

type signuprequest struct {
    email           string `json:"email"`
    firstname       string `json:"firstname"`
    lastname        string `json:"lastname"`
    password        string `json:"password"`
    confirmpassword string `json:"cpassword"`
}

func main() {
    raw := `{
    "firstname": "melly",
    "lastname": "melly",
    "email": "[email protected]",
    "password": "thepassword",
    "cpassword": "thepassword",
    "picture": "no"
    }`
    var s signuprequest
    err := json.unmarshal([]byte(raw), &s)
    if err != nil {
        fmt.println(err)
    }
    fmt.println(s)
}

输出为

{[email protected] Melly Melly thepassword thepassword}

这表明您的 json unmarshal 没有任何问题。

您的 go 版本是什么?我的是1.20。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>