Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d28920f
commit af49baa
Showing
4 changed files
with
293 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
date: 2023-09-23 | ||
categories: | ||
- programming | ||
tags: | ||
- go | ||
title: "Go gin chunked response" | ||
draft: true | ||
--- | ||
|
||
```go | ||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
log.SetFlags(log.LstdFlags | log.Lshortfile) | ||
r := gin.Default() | ||
r.GET("/test_stream", func(c *gin.Context) { | ||
w := c.Writer | ||
header := w.Header() | ||
header.Set("Transfer-Encoding", "chunked") | ||
header.Set("Content-Type", "text/html") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(` | ||
<html> | ||
<body> | ||
`)) | ||
w.(http.Flusher).Flush() | ||
for i := 0; i < 10; i++ { | ||
w.Write([]byte(fmt.Sprintf(` | ||
<h1>%d</h1> | ||
`, i))) | ||
w.(http.Flusher).Flush() | ||
time.Sleep(time.Duration(1) * time.Second) | ||
} | ||
w.Write([]byte(` | ||
</body> | ||
</html> | ||
`)) | ||
w.(http.Flusher).Flush() | ||
}) | ||
|
||
r.Run("127.0.0.1:8080") | ||
} | ||
|
||
/* | ||
browser test url: | ||
http://127.0.0.1:8080/test_stream | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
--- | ||
menu: | ||
after: | ||
name: blog | ||
name: posts | ||
weight: 5 | ||
title: Blog | ||
section: blog | ||
title: Posts | ||
--- | ||
|