gin-gonic/website

presenter e Gin

marcosPAJava opened this issue · 1 comments

Estou tentando usar um presenter no Gin

Codigo :

func TestePresenter(ctx *gin.Context) {
//var toJ *presenter.TestePresenter
var entidade *entity.TesteEntity

toJ := &presenter.TestePresenter{
	Id:        entidade.Id,
	Descricao: entidade.Descricao,
}
ctx.Header("Content-Type", "application/json")
ctx.JSON(200, toJ)

}

package entity

type TesteEntity struct {
Id int
Descricao string
}

package presenter

type TestePresenter struct { //classe Presenter
Id int json:"iddddddddddddd"
Descricao string json:"descricaoooooooooooo"
}

Erro:

2020/07/13 12:44:23 [Recovery] 2020/07/13 - 12:44:23 panic recovered:
GET /testePresenter HTTP/1.1
Host: localhost:1339
Accept: /
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Connection: keep-alive
Postman-Token: c4e1f478-68db-4a46-aab8-fcfb11335c87
User-Agent: PostmanRuntime/7.26.1

runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/runtime/panic.go:212 (0x44a9b9)
panicmem: panic(memoryError)
/usr/local/go/src/runtime/signal_unix.go:695 (0x44a808)
sigpanic: panicmem()
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/service/service.go:56 (0x9684dd)
TestePresenter: Id: entidade.Id,
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/context.go:161 (0x95343a)
(*Context).Next: c.handlersc.index
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/recovery.go:83 (0x966b3f)
RecoveryWithWriter.func1: c.Next()
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/context.go:161 (0x95343a)
(*Context).Next: c.handlersc.index
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/logger.go:241 (0x965c70)
LoggerWithConfig.func1: c.Next()
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/context.go:161 (0x95343a)
(*Context).Next: c.handlersc.index
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/gin.go:409 (0x95d215)
(*Engine).handleHTTPRequest: c.Next()
/home/marcos/Documentos/Projetos-ARQ3/dev-go-example/vendor/github.com/gin-gonic/gin/gin.go:367 (0x95c92c)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/go/src/net/http/server.go:2807 (0x6c77b2)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/go/src/net/http/server.go:1895 (0x6c312b)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/go/src/runtime/asm_amd64.s:1373 (0x464210)
goexit: BYTE $0x90 // NOP

variavel nao iniciada