- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试为基于 GIN 框架的基于 go 的 Web 应用程序添加验证。在网页上,我正在选择一个文件并提交,服务器正在处理它。在服务器端,我尝试添加验证以检查文件是否已提供。如果没有,则重定向回原始页面。
func panic(err error) {
if err != nil {
log.Println(err)
}
}
func displayTable (c *gin.Context) {
file, _ , err := c.Request.FormFile("file")
panic(err)
if file == nil {
log.Println("File is nil.")
log.Println(err)
log.Println("*****")
c.HTML(http.StatusInternalServerError, "index.tmpl", gin.H{
"title": "Select the input file","error" : "Please select the input file.",
})
} else {
defer file.Close()
}
filename := strconv.FormatInt(time.Now().Unix(),10)
out, err := os.Create("./tmp/"+filename+".xml")
panic(err)
defer out.Close()
_, err = io.Copy(out, file)
panic(err)
xmlFile, err := os.Open("./tmp/"+filename+".xml")
panic(err)
defer xmlFile.Close()
// Other Implementation Details
}
即使在提供了处理之后,我仍然在 go 代码中感到 panic 。请让我知道我在实现中遗漏了什么。
谢谢。
http: no such file
File is nil.
http: no such file
*****
2015/08/04 13:19:10 Panic recovery -> runtime error: invalid memory address or nil pointer dereference
c:/go/src/runtime/panic.go:387 (0x414d36)
c:/go/src/runtime/panic.go:42 (0x4142a5)
c:/go/src/runtime/os_windows.go:42 (0x414066)
c:/go/src/io/io.go:362 (0x45268f)
D:/code/src/exmp/serverexmaple.go:45 (0x40168f)
displayTable: _, err = io.Copy(out, file)
D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
(*Context).Next: c.handlers[c.index](c)
D:/code/src/github.com/gin-gonic/gin/logger.go:56 (0x4ac490)
func.007: c.Next()
D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
(*Context).Next: c.handlers[c.index](c)
D:/code/src/github.com/gin-gonic/gin/recovery.go:43 (0x4acc80)
func.009: c.Next()
D:/code/src/github.com/gin-gonic/gin/context.go:95 (0x49f8ea)
(*Context).Next: c.handlers[c.index](c)
D:/code/src/github.com/gin-gonic/gin/gin.go:292 (0x4a46d5)
(*Engine).handleHTTPRequest: context.Next()
D:/code/src/github.com/gin-gonic/gin/gin.go:273 (0x4a4459)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
c:/go/src/net/http/server.go:1703 (0x468415)
c:/go/src/net/http/server.go:1204 (0x466408)
c:/go/src/runtime/asm_386.s:2287 (0x438ea1)
最佳答案
panic
。它会让所有了解 panic
工作原理的人感到困惑。FormFile
返回接口(interface),因此如果您想使用 nil 检查它或使用第二个参数(其类型可用),则必须将其转换为底层结构。关于validation - gin-gonic 中的多部分文件上传验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31800843/
我有很多路线,从 gin.Default() 开始使用 Gin (默认情况下为所有路由启用日志记录和恢复)。但是有一条路由(即 /health )每 5 秒被 ping 一次。在不更改大部分代码的情况
gin 是目前 Go 里面使用最广泛的框架之一了,弄清楚 gin 框架的原理,有助于我们更好的使用 gin。这个系列 gin 源码阅读会逐步讲明白 gin 的原理,欢迎关注后续文章。 gin
我从 gin 文档中了解到,您可以将 json 绑定(bind)到类似 的结构 type Login struct { User string `form:"user" json:"u
我正在使用 API,GET 和 POST 工作正常,除非我尝试通过其 ID 获取选择记录(例如/articles/2)。文章存在,当通过/articles 路径检索所有记录时,我得到了正确的响应。这是
我找不到在 UNIX 中正确执行此代码的方法: package main import ( "time" "github.com/gin-gonic/gin" "net/http" ) t
我尝试使用作为 Golang 框架的 Gin。 https://github.com/gin-gonic/gin 我从官方github上复制了示例代码。 就像这样。 package main impo
下面访问gin-gonic服务器时,HTTP客户端应该收到code 500,却收到code 200。 package main import ( "github.com/gin-contrib
我使用 Guice/Gin 设计了一个新项目,因此我可以使我们的代码更加模块化和可交换,尤其是在测试时。 但是,我无法弄清楚如何在实践中完成这项工作。我的印象是我可以在我的测试中创建一个新的 Gin/
我正在尝试在 golang:alpine docker 镜像上运行一个非常简单的 go/gin 程序。这无法运行/构建。 运行容器: docker run -it --rm golang:1.10.1
本文介绍如何通过 rk-boot 快速搭建静态文件下载 Web 服务。什么是 静态文件下载 Web UI?通过配置文件,快速搭建可下载文件的 Web 服务。 请访问如下地址获取完整教程: r
这是我的一个例子 - public class GinDemoPresenter implements Presenter { private View view; @Inject
我正在尝试使用一种干净的方法,使用传递给我的类的父上下文来优雅地停止我的gin服务器。 这是我当前的代码 有没有更清洁的方法可以做到这一点?感觉像很多样板代码和不必要的例如使用无限循环完成如此简单的任
我有以下代码 public class AppGinModule extends AbstractGinModule{ @Override protected void configu
我正在使用OPTIONS方法获得204,但是似乎没有碰到终点 只需构建一个简单的文件上传端点,如下所示: package main import ( "cloud.google.com/go
在对其数据执行验证后,我试图用它的数据恢复上下文。我需要数据在下一个函数中根据需要继续移动。 我是 golang 的新手,下面的代码是我所能做的。非常感谢任何帮助和更好的方法。 提前致谢。 验证中间件
有一个使用gin-gonic框架以golang编写的服务。 我只想将application/json支持为mime类型,并且如果它始终以UTF-8格式运行,那就太好了。如果服务的业务逻辑将获得不同编码
我试图在我的 Handler 上进行一些处理后传递一些变量功能。我该怎么办 redirect到一个新页面并将一些 json 变量传递给这个新模板? // main package func main(
我运行关于文件上传的 gin 示例,这个 repo 来自 https://github.com/gin-gonic/examples/tree/5898505356e9064c49abb075eae8
我收到以下错误消息: controllers/user.go:4:2: cannot find package "(underscore)/home/ubuntu/goapi/src/github.c
我需要在中间件函数中操作响应数据。假设我有产品处理程序和客户处理程序。产品处理程序返回产品列表,客户返回客户列表。在中间件函数中,我想将这些响应转换为 ApiResponse 结构。 type Api
我是一名优秀的程序员,十分优秀!