- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我的 Go 编程新手遇到问题,例如:无效的内存地址或 nil 指针取消引用
有时我可以解决问题,但这让我感到困惑。这是处理程序级别的代码,我试图实现###p.repo.UpdateProfile()和来自 r.body 解码的数据
//UpdateProfile handler
func (p *Profile) UpdateProfile(w http.ResponseWriter, r *http.Request) {
var (
errForm models.ErrorForm
resp models.Response
respError models.ErrorResponse
errField models.ErrField
data *models.EditProfile
)
userid := r.Context().Value(viper.GetString("token.userid"))
errDecode := json.NewDecoder(r.Body).Decode(&data)
errPayload := p.repo.UpdateProfile(r.Context(), data, userid)
if errPayload.Error() == "username_exist" {
respError.Message = "username already taken"
respError.Status = 422
lib.ResJSON(w, respError.Status, respError)
return
}
lib.Catch(errPayload)
resp.Data = ""
resp.Message = "Success"
resp.Status = 200
lib.ResJSON(w, resp.Status, resp)
}
func (m *mysqlProfileRepo) UpdateProfile(ctx context.Context, p *models.EditProfile, userid interface{}) error {
query := ""
var checkexist int
row1, err1 := m.Conn.QueryContext(ctx, query, p.Username)
if err1 != nil {
return err1
}
for row1.Next() {
if errSc1 := row1.Scan(&checkexist); errSc1 != nil {
return errors.New("error_scan_db")
}
}
if checkexist != 0 {
return errors.New("username_exist")
}
query1 := ""
query2 := ""
stmts := []*lib.PipelineStmt{
lib.NewPipelineStmt(query1, p.Image, p.Location, p.Link, p.Bio, p.Birthday, userid),
lib.NewPipelineStmt(query2, p.Username, p.Fullname, userid),
}
errTrx := lib.WithTransaction(m.Conn, func(tx lib.Transaction) error {
_, errRunPipe := lib.RunPipeline(tx, stmts...)
return errRunPipe
})
if errTrx != nil {
return errTrx
}
return nil
}
panic: runtime error: invalid memory address or nil pointer dereference
2019/07/21 13:27:36 goroutine 8 [running]:
runtime/debug.Stack(0xc00028e100, 0x16b1a20, 0xc000094040)
/usr/local/Cellar/go/1.12.6/libexec/src/runtime/debug/stack.go:24 +0x9d
github.com/go-chi/chi/middleware.Recoverer.func1.1(0xc00028e100, 0x16b6be0, 0xc000256280)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:25 +0x9c
panic(0x155e7a0, 0x1a66a10)
/usr/local/Cellar/go/1.12.6/libexec/src/runtime/panic.go:522 +0x1b5
audioo_api/handler.(*Profile).UpdateProfile(0xc000020030, 0x16b6be0, 0xc000256280, 0xc00028e200)
/Users/rizaldinurmuhammad/go/src/audioo_api/handler/profile.go:73 +0x524
net/http.HandlerFunc.ServeHTTP(0xc000020050, 0x16b6be0, 0xc000256280, 0xc00028e200)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Authenticate.func1.1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/auth.go:48 +0x71e
net/http.HandlerFunc.ServeHTTP(0xc00000e0a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*ChainHandler).ServeHTTP(0xc000066140, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/chain.go:31 +0x52
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020040, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5f80, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e0e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc0001db880, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a59e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:70 +0x451
github.com/go-chi/chi.(*Mux).Mount.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:292 +0x127
net/http.HandlerFunc.ServeHTTP(0xc00000e180, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).routeHTTP(0xc0000a5920, 0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:425 +0x27f
net/http.HandlerFunc.ServeHTTP(0xc000020090, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
audioo_api/lib/mid.Limit.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/audioo_api/lib/mid/limit.go:79 +0xe5
net/http.HandlerFunc.ServeHTTP(0xc00000e1a0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/cors.(*Cors).Handler.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/cors/cors.go:201 +0x1a4
net/http.HandlerFunc.ServeHTTP(0xc00000e1c0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.Recoverer.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/recoverer.go:35 +0x9f
net/http.HandlerFunc.ServeHTTP(0xc00000e1e0, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RedirectSlashes.func1(0x16b6be0, 0xc000256280, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/strip.go:53 +0x202
net/http.HandlerFunc.ServeHTTP(0xc00000e200, 0x16b6be0, 0xc000256280, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.(*Compressor).Handler.func1.1(0x1f69180, 0xc000256240, 0xc00028e100)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/compress.go:190 +0x254
net/http.HandlerFunc.ServeHTTP(0xc00000e2c0, 0x1f69180, 0xc000256240, 0xc00028e100)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi/middleware.RequestLogger.func1.1(0x16b78a0, 0xc000226000, 0xc00028e000)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/middleware/logger.go:46 +0x291
net/http.HandlerFunc.ServeHTTP(0xc00009a210, 0x16b78a0, 0xc000226000, 0xc00028e000)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1995 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc0000a5920, 0x16b78a0, 0xc000226000, 0xc000154000)
/Users/rizaldinurmuhammad/go/src/github.com/go-chi/chi/mux.go:82 +0x294
net/http.serverHandler.ServeHTTP(0xc00009c410, 0x16b78a0, 0xc000226000, 0xc000154000)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc0002ce640, 0x16b8aa0, 0xc000067340)
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
/usr/local/Cellar/go/1.12.6/libexec/src/net/http/server.go:2884 +0x2f4
我想知道这个问题与 r.body 有关,但我不知道如何弄清楚
最佳答案
根据 Body io.ReadCloser 的包文档:
For client requests, a nil body means the request has no body, such as a GET request. For server requests, the Request Body is always non-nil.
你说,它是一个服务器,所以,它总是非 nil,因此错误出现在你将指针传递给 nil 指针的 Decode
调用中。
data
是一个nil
指针,&data
是一个指向nil
指针的指针。
如果数据是非接口(interface)类型的解决方案,除非models
包明确禁止new(EditProfile)
:
data = new(models.EditProfile)
errDecode := json.NewDecoder(r.Body).Decode(data)
如果数据是一个接口(interface),首先为其分配一个真正的值,然后再次传递而不引用Decode
:errDecode := json.NewDecoder(r.Body).Decode(数据)
在后一种情况下,还要仔细检查您是否声明了正确的类型 *models.EditProfile
。
关于go - panic : runtime error: invalid memory address or nil pointer dereference || r. 人体模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130942/
*&x 是否总是等于 x?如果不是,什么时候不是? &*x 是否总是等于 x?如果不是,什么时候不是? 我的猜测是 (1) 总是正确的,但 (2) 并不总是正确的,因为 x 可能并不总是一个指针,所以
这段代码: int main(char[][] args) { MyObject obj; obj.x; return 0; } 给我:Error: null dereference in
我想减少使用 XMLHttpRequest() 从服务器获取信息的 I/O 开销或ActiveXObject()视情况而定,通过创建一个通用函数来执行提取,然后使用 sessionStorage. 存
我对以下代码有疑问: #!/usr/bin/perl use strict; use warnings; my %dmax=("dad" => "aaa","asd" => "bbb"); my %d
int main() { char test[15] = "Hallo World"; test[1] = *"e"; } 要将第二个字母更改为 e,我必须遵循字符串 "e"。我找不到
我正在尝试通过实现一些基本数据结构来学习 Rust。在本例中,是一个 Matrix。 struct Matrix { pub nrows: uint, pub ncols: uint, p
Editor's note: This code example is from a version of Rust prior to 1.0 and is not valid Rust 1.0 co
我刚刚阅读完 rust-lang.org 上的生命周期指南并尝试实现该示例(但使用通用枚举来增加一点复杂性)。 enum PositionInfo { Position(T, T), } en
在 JavaScript 中,如何取消引用函数返回的对象? 例如: var tmp = getTextProperties(); font = tmp.font; size = tmp.siz
我是 C++ 的新手,作为练习(也许最终是 .Net 实用程序),我正在做一个指针包装器(实际上是在 C++/CLI 中,但这也适用于 C++)。这个指针包装器(称为 Apont)目前的行为就像一个指
表达式 &ptr->fld不代表取消引用,而是应将其视为 (uint32_t)ptr + offsetof (ptr, fld) .我确信 GCC 做了这种简化,但我找不到代码中的位置。 以上结果为
我现在正在试验 Rust,我真的被各种随机的编译器错误绊倒了,比如这个: error: cannot move out of dereference of `&`-pointer return
我是 Rust 的新手,正在尝试编写命令行实用程序作为学习的一种方式。 我正在获取 args 的列表并尝试匹配它们 let args = os::args() //some more code mat
我目前正在为 C 语言开发一个简单的克隆检测器,用 C++ 编写,并且不断地问自己关于效率和如何优化 C++ 代码的问题。 我有一个问题是关于如何有效地传递结构。如果给出类似于以下内容的结构: typ
我在 C# 中有一些代码使用这样的结构: ArrayList addrs = new ArrayList(); byte[] addr = new byte[8]; while (oneWire.Se
这个问题在这里已经有了答案: What's the meaning of * and & when applied to variable names? (1 个回答) 关闭 6 年前。 刚接触 C
在 boost::filesystem 中,path 类总是尝试解引用符号链接(symbolic link)。许多 API 都是为了让符号链接(symbolic link)看起来不可见。我猜他们下面的
我正在尝试使用 C# 委托(delegate)实现撤消功能。基本上,我有一个 UndoStack,它维护一个实现每个撤消操作的委托(delegate)列表。当用户选择 Edit:Undo 时,此堆栈弹
我有一个工作正常的记录器,但在内存分配方面产生了相当多的开销。下面的 Debug() 函数不是故意打印的,因为 logOutputLevel 不够高。 var logOutputLevel = 2 f
我有这个 C 代码: #include #include
我是一名优秀的程序员,十分优秀!