package main

import (
"fmt"
"net/http"
//"net/url"
"github.com/drone/routes"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"encoding/json"
) var db = &sql.DB{} func init(){
var err interface{}
db, err = sql.Open("mysql", "root:123@tcp(localhost:3306)/mytest?charset=utf8")
if err!=nil{
fmt.Println("数据库连接出错")
}else{
fmt.Println("suc sql connect")
}
} type BaseRes struct{
Code int
Msg string
Data interface{}
} func (b BaseRes) sucSet(m string,c int) BaseRes{
b.Code=c
b.Msg=m
return b //这里要返回出去才能被外界访问,因为这是一个闭包
} type Article struct{
Title string
Author string
Time string
Content string
} type ArticleTwo struct{
Id int
Title string
Content string
} func testRoute(w http.ResponseWriter,r *http.Request){
var sid Article
sid.Title="第一女首富的最好时刻又来了"
sid.Author="周聪明"
sid.Time="2014-12-03"
sid.Content="十一年前的今天,当年的胡润百富榜发布,49岁的张茵拥有财富270亿元,成为中国第一位女首富,也是世界上最富有的女性白手起家者。当胡润公布首富人名时,场下只有两三"
var sendData BaseRes
sendData.Code=10000
sendData.Msg="success"
sendData.Data=sid
res,_:=json.Marshal(sendData)
fmt.Fprintln(w, string(res))
} func testRouteTwo(w http.ResponseWriter,r *http.Request){
rows, _ := db.Query("select * from article")
var a ArticleTwo
var arr []ArticleTwo //定义一个arr的数组用来存储ArticleTwo类型的数据
for rows.Next() {
rows.Scan(&a.Title, &a.Content, &a.Id) //存储ArticleTwo类型的数据
arr=append(arr,a) //循环加入数组中
}
var b BaseRes
b.Data=arr
b=b.sucSet("suc",10000)
res,_:=json.Marshal(b)
fmt.Fprintln(w, string(res))
} func main() {
fmt.Println("正在启动WEB服务...")
var mux *routes.RouteMux = routes.New()
mux.Get("/",testRoute)
mux.Get("/two",testRouteTwo) //http.Handle("/", mux)
http.ListenAndServe(":8088", mux)
fmt.Println("服务已停止")
}

  

go api json 输出尝试的更多相关文章

  1. Jackson如何使JSON输出变得优雅?

    本篇文章翻译自:How to enable pretty print JSON output (Jackson) 在这篇文章中,我们将教你如何利用Jackson Library在控制台或者JSP页面优 ...

  2. Springmvc 配置json输出的几种方式

    Spring MVC 3.0 返回JSON数据的几种方法: 1. 直接 PrintWriter 输出 2. 使用 JSP 视图 3. 使用Spring内置的支持 // Spring MVC 配置 &l ...

  3. FastJson/spring boot: json输出方法二

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  4. FastJson/spring boot: json输出

    1.引入FastJson依赖包 <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> ...

  5. php api 接口输出json 数据

    页面调用接口,简单写个api 试试 如下 <?php $arr = array( array('url'=>'https://baidu.com'), array('map'=>'h ...

  6. 解决ASP.NET Web API Json对象循环参考错误

    前言 一般我们在开法 ASP.NET Web API 时,如果是使用 Entity Framework 技术来操作数据库的话,当两个 Entity 之间包含导览属性(Navigation Proper ...

  7. 快速搭建REST API——json server

    一:全局安装json-server npm install json-server -g 二:在自己项目跟目录下存放mock/data.json,json内容如下: { "roles&quo ...

  8. TP5内部异常API数据输出的自定义方法编写

    需求:利用postman进行请求api接口过程中 关于一些数据输出异常的情况下 我们希望通过自己编写一些类和方法 实现便于后端人员进行根据提示进行调试处理! 以下测试的时候 请设置 app_debug ...

  9. python 格式化 json输出

    利用python格式化json 字符串输出. $ echo '{"json":"obj"}' | python -m json.tool 利用python -m ...

随机推荐

  1. Flutter(一)安装配置和几个注意点

    Flutter(一)安装配置和几个注意点 记住要仰望星空,不要低头看脚下.无论生活如何艰难,请保持一颗好奇心.你总会找到自己的路和属于你的成功. 愿您的来世灵魂依旧,躯体康健. 一句话来形容Flutt ...

  2. python基础--windows环境下 安装python2和python3

    一.  python 安装 1. 下载安装包 1 2 3 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi    # 2 ...

  3. 关于空指针NULL、野指针、通用指针 (转)

    reference:https://www.cnblogs.com/losesea/archive/2012/11/16/2772590.html 首先说一下什么是指针,只要明白了指针的含义,你就明白 ...

  4. Alpha冲刺5

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9989898.html 作业博客:https://edu.cnblogs.com/campus/ ...

  5. makeObjectsPerformSelector对数组中的对象发送消息执行对象中方法

    - (void)makeObjectsPerformSelector:(SEL)aSelector; - (void)makeObjectsPerformSelector:(SEL)aSelector ...

  6. Vxlan基础理解

    一 . 为什么需要Vxlan   1. vlan的数量限制    4096个vlan远不能满足大规模云计算数据中心的需求   2. 物理网络基础设施的限制    基于IP子网的区域划分限制了需要二层网 ...

  7. SQL注入之Sqli-labs系列第三十四关(基于宽字符逃逸POST注入)和三十五关

    开始挑战第三十四关和第三十五关(Bypass add addslashes) 0x1查看源码 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理. if(isset($_ ...

  8. 构建gulp项目

    express是node.js中的构建工具,如果需要使用express构建,首先需要安装express. 构建一个项目: |-- app| |-- css| |-- js| | `-- class| ...

  9. mlsql 基本操作

    数据库的操作: 1.创建 create databases python_test_01(库名,自定义)chaeset = utf8; 2.删除 drop database python_test_0 ...

  10. Shell脚本、Shell脚本结构、date命令的用法、变量

    1.Shell脚本: shell是一种脚本语言 目的:可以实现自动化运维,能大大增加运维的效率.2.Shell脚本结构:   #!/bin/bash  以#!/bin/bash开头,即以/bin/ba ...