go語言中beego框架資料輸出和返回json詳解
beego框架中資料輸出
1.直接輸出字串 beego.Controller.Ctx.WriteString(“字串”)
func (ctx *Context) WriteString(content string) {
ctx.ResponseWriter.Write([]byte(content))
}
2.模板資料輸出 beego.Controller.Data[“名字”]=資料
beego.Controller.TplName=模板檔案
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.TplName = "hello.tpl"
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Email"] = " [email protected]"
c.TplName = "hello.tpl"
}
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello World!</h1>
Contact me:
<a class="email" href="mailto:{{.Email}}">{{.Email}}</a> //動態模板輸出
</body>
</html>
3.json格式輸出
beego.controller.ServeJSON()
通過把要輸出的資料放到Data[“json”]中,然後呼叫ServeJSON()進行渲染,就可以把資料進行JSON序列化輸出。
type JSONStruct struct { Code int Msg string } func (c *MainController) Get() { mystruct := &JSONStruct{0, "hello"} c.Data["json"] = mystruct c.ServeJSON() }
4.xml格式輸出 beego.controller.Data[“xml”]=資料
beego.controller.ServeXML()
type XMLStruct struct {
Code int
Msg string
}
func (c *MainController) Get() {
mystruct := &XMLStruct{0, "hello"}
c.Data["xml"] = mystruct
c.ServeXML()
}
5.jsonp呼叫 beego.controller.Data[“jsonp”]=資料
beego.controller.ServeJSONP()
type JSONStruct struct {
Code int
Msg string
}
func (c *MainController) Get() {
mystruct := &JSONStruct{0, "hello"}
c.Data["jsonp"] = mystruct
c.ServeJSONP()
}
相關推薦
go語言中beego框架資料輸出和返回json詳解
beego框架中資料輸出 1.直接輸出字串 beego.Controller.Ctx.WriteString(“字串”) func (ctx *Context) WriteString(content string) { ctx.ResponseWrit
Go語言及Beego框架環境搭建
在開始環境搭建之前,我們先一起來看看: Go有什麼優勢: 不用虛擬機器,它可直接編譯成機器碼,除了glibc外沒有其他外部依賴,部署十分方便,就是扔一個檔案就完成了。 天生支援併發,可以充分的利用多核,很容易實現併發。 25個關鍵字,但是表達能力很強大,幾乎支援大多數你在其他語言見過的特性:繼承、過載、物件
beego框架使用環境搭建深入剖析詳解
在搭建beego的時候,注意需要下載git,然後安裝 goget−ugithub.com/astaxie/beegogoget−ugithub.com/astaxie/beego go get -u github.com/beego/bee
大資料IMF傳奇行動絕密課程第64課:Spark SQL下Parquet的資料切分和壓縮內幕詳解
Spark SQL下Parquet的資料切分和壓縮內幕詳解 1、Spark SQL下的Parquet資料切分 2、Spark SQL下的Parquet資料壓縮 parquetBlocksize總體上講是壓縮後的大小 private static fina
go語言中bytes包的常用函數,Reader和Buffer的使用
函數 tolower 參數 比較 新建 切分 any dst 操作 bytes中常用函數的使用: package main; import ( "bytes" "fmt" "unicode" ) //bytes包中實現了大量對[]byte操作
Go語言中Printf和Println
1. Printf 如果是空字串,輸出了空字串的 雙引號 "" 例如 func variableZeroValue(){ var a int var s string fmt.Printf("%d %q\n", a, s) } 輸出結果
go語言學習-beego框架學習bee在windows和linux下的安裝
windows下的安裝 windows下的安裝和linux下其實是相同的命令 go get github.com/beego/bee //或者使用 go install github.com/beego/bee 在windows下安裝完成之後需要在 環
Go語言中new()和 make()的區別詳解
概述Go 語言中的 new 和 make 一直是新手比較容易混淆的東西,咋一看很相似。不過解釋兩者之間的不同也非常容易。new 的主要特性複製程式碼程式碼如下:func new(Type) *Type官方文件對於它的描述是:複製程式碼程式碼如下:內建函式 new 用來分配記憶
Go語言及Web框架Beego環境搭建
本文涉及軟體均以截至到2013年10月12日的最新版本為準 1、 相關軟體準備: 1) go1.6.windows-amd64.msi,對應64位windows系統安裝使用 下載地址:http://www.golangtc.com/download 3) lite
Go語言及Web框架Beego環境無腦搭建
本文涉及軟體均以截至到2013年10月12日的最新版本為準 1、 相關軟體準備: 1) go1.2rc1.windows-386.msi,對應32位windows系統安裝使用 下載地址: 2) go1.2rc1.windows-amd64.msi,對應32位windows系統安裝使用 下
【原】Go語言及Web框架Beego環境無腦搭建
本文涉及軟體均以截至到2013年10月12日的最新版本為準 1、 相關軟體準備: 1) go1.2rc1.windows-386.msi,對應32位windows系統安裝使用 下載地址: 2) go1.2rc1.windows-amd64.msi,對應32位windows系統安裝使用 下載地
(Go)07.Go語言中strings和strconv包示例代碼詳解
dex 實現 light div 重復字符 轉換 trim 漢字 大小寫 1.strings使用 前綴和後綴 HasPrefix判斷字符串s是否以prefix開頭: 示例: package main import ( "fmt" "strings"
GO語言中printf和println的區別
作為兩種輸出方法,兩者之間除了換行之外還有其他區別。 Println :可以打印出字串,和變數 Printf : 只可以打印出格式化的字串,可以輸出字串型別的變數 //例如: a := 10 p
C語言中printf用%d輸出float型別資料,或以%f輸出int型資料的結果
1.測試程式及結果 程式#include"stdio.h" int main() { float a = 7.5, b = 1.23, c = 1.24, d = 1.25; double a1 = 7.5, b1 = 1.23, c1 = 1.24, d1 = 1.
[轉]Go語言中的make和new
接受 默認值 讓我 錯誤提示 兩個 無效 target 在哪裏 new 前言 本文主要給大家介紹了Go語言中函數new與make的使用和區別,關於Go語言中new和make是內建的兩個函數,主要用來創建分配類型內存。在我們定義生成變量的時候,可能會覺得有點迷惑,其實他們的
Go語言中的make和new
內存 sharp 補充 UNC 地址 賦值 fmt signal bubuko 相同點: make和new都是用來創建分配類型內存的。 不同點: 先看下面的代碼 package main import "fmt" func main(){ var i
go語言中container容器數據結構heap、list、ring
func 都是 slice 比較 next println div less 插入元素 heap堆的使用: package main import ( "container/heap" "fmt" ) type IntHeap []int
go語言中os/exec包的學習與使用
pipe 設置 int 管道 baidu 標準輸出 byte Go語言 println package main; import ( "os/exec" "fmt" "io/ioutil" "bytes" ) func main() {
django模板語言中的extends,block和include
ima django 一行 。。 log 語言 extend img 需要 extends和block一起用 它們用於母版和子版的繼承 在母版html中將一些需要替換的部分用{% block xxx %}。。。{% endblock %}括起來, 在子版html中,
go 語言中的args多參數的傳遞
color ... ++ 使用 i++ int light style imp 多個參數類型一樣的時候,如何進行傳遞 package main import "fmt" func main() { myfunc(1) myfunc(5,6) } // 要是使用