gin-gonic / website Goto Github PK
View Code? Open in Web Editor NEWOfficial website and document for Gin
Home Page: https://gin-gonic.com/
License: MIT License
Official website and document for Gin
Home Page: https://gin-gonic.com/
License: MIT License
The Testing is not clear enough when we need to test more than one route.
For instance, if we have multiple routes in our project, such as this
func setupRouter() *gin.Engine {
router := gin.Default()
router.GET("/albums", getAlbums)
router.POST("/album", addAlbum)
router.GET("/albums/:id", getAlbumById)
return router
}
based on the documentation, how can we test all this routes and also avoid calling setupRouter for every test function? as I did in the project above
func TestGetAlbumsRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/albums", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "[{\"id\":\"1\",\"title\":\"Blue Train\",\"artist\":\"John Coltrane\",\"price\":56.99},{\"id\":\"2\",\"title\":\"Jeru\",\"artist\":\"Gerry Mulligan\",\"price\":17.99},{\"id\":\"3\",\"title\":\"Sarah Vaughan and Clifford Brown\",\"artist\":\"Sarah Vaughan\",\"price\":39.99}]", w.Body.String())
}
func TestGetAlbumById(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/albums/2", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.JSONEq(t,
"{\"id\":\"2\",\"title\":\"Jeru\",\"artist\":\"Gerry Mulligan\",\"price\":17.99}",
w.Body.String())
}
The documentation could have just the one example with multiple routes so this scenario would be more wide explained and help more code with basic apis.
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
The method of getting file array from request form is wrong.
files := form.File["upload[]"]
->
files := form.File["upload"]
need to sync README
So, we could remove it, or use other instead.
Sometimes the official website can't be accessed, so can you put the compiled static pages together in a folder and upload them to git repository?Thank you.
miss the value of dst
please update the site, the current is so old-looking and made me won't install Gin
I tried loading the site multiple times today, but it says it's unavailable.
the documentation doesnt describe any of the functions, classes or methods in it.
The only thing that the documentation gives you, is examples which you have to understand by yourself ehich isnt the point.
A documentation must be descriptive, clear, show every single aspect of the package WITH expamples which this one isnt doing.
The documentation is poorly written and unclear.
You should consider refactoring the documentation abt the framework
I'm relatively new to Gin, and so far I love the framework.
My background is mostly based in PHP working with Laravel, and part of what makes Laravel so great to get started with is the documentation. I know that the Gin repository contains a very in-depth README, however, I feel that it would benefit the framework vastly by having an in-depth documentation for those who are new to the framework or those that are experienced to access.
This is something that I would personally be very happy to work on.
It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter.
up to 40 times faster thanks to httprouter. Means 由于 httprouter,速度提高了40倍。
Not 比 httprouter 的速度快了40倍, which means 40 times faster than httprouter.
Originally posted by @ganlvtech in #28
请问这个demo是不是缺少文件啊?
/Users/liping/go/pkg/mod/github.com/gin-gonic/[email protected]/binding/msgpack.go:12:2: unknown import path "github.com/ugorji/go/codec": ambiguous import: found github.com/ugorji/go/codec in multiple modules:
github.com/ugorji/go v1.1.4 (/Users/liping/go/pkg/mod/github.com/ugorji/[email protected]/codec)
github.com/ugorji/go/codec v0.0.0-20180831062425-e253f1f20942 (/Users/liping/go/pkg/mod/github.com/ugorji/go/[email protected])
hi, i'm new to golang and this framework.
I'm trying to run a starting template following document.
However, I encounter some warning :
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
I want to know more detail. So I follow into the document, but #readme-don-t-trust-all-proxies is nowhere to be found.
I know func (*Engine) SetTrustedProxies could possibly solve the warning. But I still wanna know why I should do it as "don-t-trust-all-proxies"?
ps: I tried google "don't trust all proxies" but nothing specific. And I'm a newbee and this is my first time making issue. Please feel free to correct me about anything.
In the Using Middleware Example the code shows a code block after the call to authorized.Use(AuthRequired())
.
What is the purpose of putting the code inside its own block? The block seems to be unnecessary.
the code in https://gin-gonic.com/zh-cn/docs/examples/jsonp/
r.GET("/JSONP?callback=x", func(c *gin.Context) {
data := map[string]interface{}{
"foo": "bar",
}
// callback 是 x
// 将输出:x({\"foo\":\"bar\"})
c.JSONP(http.StatusOK, data)
})
need to be:
r.GET("/JSONP", func(c *gin.Context) {
data := map[string]interface{}{
"foo": "bar",
}
// callback 是 x
// 将输出:x({\"foo\":\"bar\"})
c.JSONP(http.StatusOK, data)
})
I need to create an endpoint in a group that returns me a list of the path of my application but I can't find the method that returns this information to me, the handlenames I've already found
*gin.Context.HandlerNames()
*gin.Context.HandlerRoutes()???????
https://gin-gonic.com/docs/quickstart/
But Govendor project has been archived:
We should update the documentation accordingly:
https://blog.golang.org/migrating-to-go-modules
c.PostForm("")
获取post参数,有时他会取缓存。
I will create the zh-tw
folder for Traditional Chinese language. Rename the cn
to zh-cn
.
cc @thinkerou
Hi, @javierprovecho @appleboy @isgj @dlaub3
As title and optimize later on.
Gin website build based on https://github.com/census-instrumentation/opencensus-website and not use git submodule but copy docdock theme.
Use docsy
theme.
The feature https://gin-gonic.com/introduction/ come from https://gin-gonic.github.io/gin/, right?
TODO:
favicon svg and some image for Gin
improve document, because only have example code have not explain
improve directory, like: binding dir, rendering dir, middleware dir, and so on
optimize/update header nav / footer nav? update color?
Chinese translation dir
use git submodule?
add disqus?
In quickstart, it says, "Use a vendor tool like Govendor". But go module is replacing Govendor. Maybe you can update the documentation.
See the result: https://gin-gonic.com/search/?q=POST
Would you be willing to remove this Google Fonts from this website? There are better alternatives available
Some reputable alternatives can be seen here: https://switching.software/replace/google-fonts/
os: windows 10
hugo version: 0.54.0
$ hugo server
Building sites … Total in 571 ms
Error: Error building site: "%root%\content\ja\_index.html:6:5": failed to extract shortcode: template for shortcode "blocks/cover" not found
Same error occured using
$ npm install
$ hugo
Am I missing any files?
The testing example is only showing how to test the GET method, but there is not way to show how to do the POST and where can we send the body data!
Now have had English, Chinese and Japanese, but Chinese and Japanese only one page, I hope they also multi sub page like English version.
So, when visit https://gin-gonic.com/ is English version, and visit https://gin-gonic.com/ja/ is Japanese version, and visit https://gin-gonic.com/cn/ is Chinese version, and so on.
thanks!
directory change for supporting multi-language
Chinese
Japanese
建议添加类似 Query 和 post form 的使用案例。如:
POST /form_post?message=message&nick=nick
Content-Type: application/x-www-form-urlencoded
It would be nice to use a theme with syntax highlighting. It makes reading code samples easier.
https://gin-gonic.com/docs/ has a "Documentation" link that directs to itself. It would make more sense to link it to either https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc or https://godoc.org/github.com/gin-gonic/gin.
gin: gin-gonic/gin#1264
I created the project in my workspace and make the build by using
~/go/src/hello1$ go build
-> and run the using below the commend
~/go/src/hello1$ ./hello1
-> It is working fie
[GIN-debug] Listening and serving HTTP on :8080
But I opened the new terminal and run the same means it is not working
dst := "./" + file.Filename
我是一个新手,刚学gin。
请问如何记录接口的入参和出参,这对于排查问题很有帮助。
We provide some API usage examples and lists some public known Gin’s user.
I believe the link of [public known Gin's user] is wrong
I want to write template into bytes.Buffer,like
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
log.Println(err)
return
}
result := tpl.String()
i want write this use gin,how to do?
If you open the website at https://gin-gonic.com/docs/quickstart/
the page show wrong numbering for content, everything is numbered 1
I check the file in https://github.com/gin-gonic/website/tree/master/content/en/docs/quickstart
already use correct numbering
生成了对应的key,pem,配置好了,但是说是不安全的,也同意继续了。
但是app.js并不是推送的,而是再次请求的,network两个请求。
看了一下命令行有错误输出,问下Failed to push: feature not supported这个问题怎么解决。
2023/04/11 22:50:18 Failed to push: feature not supported
[GIN] 2023/04/11 - 22:50:18 | 200 | 66.39µs | ::1 | GET "/"
[GIN] 2023/04/11 - 22:50:18 | 200 | 227.806µs | ::1 | GET "/assets/app.js"
Router r.GET("/admin:MODE", HandleAdmin)
Hander mode := c.Param("MODE")
HTTP GET http://127.0.0.1:8088/v2d0/admin:offline
Result :offline
is BUG?
I run AsciiJSON example. but will output
{
"lang": "GO语言",
"tag": "
"
}
my env:
go version:1.6.2
gin: 1.6.3
os: mac OS
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.