package main
import "fmt"
import "net/http" import "net/url" import "github.com/drone/routes" import _ "github.com/go-sql-driver/mysql" import "database/sql" import "github.com/garyburd/redigo/redis"type Vr struct {
name string vrurl string belong string vrid int }func getuser(w http.ResponseWriter, r *http.Request) {
var params url.Values = r.URL.Query() var uid string = params.Get(":uid") fmt.Fprintln(w, "get a user ", uid, " success!") } func getuserAndAge(w http.ResponseWriter, r *http.Request) { var params url.Values = r.URL.Query() var uid string = params.Get(":uid") var age string = params.Get(":age") fmt.Fprintln(w, "get a user ", uid, " success! age is ", age) } func edituser(w http.ResponseWriter, r *http.Request) { var params url.Values = r.URL.Query() var uid string = params.Get(":uid") fmt.Fprintln(w, "edit a user ", uid, " success!") }func main() {
//链接 redis 服务器 c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println(err) return } else { fmt.Println("connect ok") }v, err := c.Do("SET", "name", "red")
if err != nil { fmt.Println(err) return } fmt.Println(v) v, err = redis.String(c.Do("GET", "name")) if err != nil { fmt.Println(err) return } fmt.Println(v) //values, _ := redis.Values(c.Do("lrange", "redlist", "0", "100"))
for _, v1 := range values {
fmt.Println(string(v1.([]byte))) }// 或者
//var v1 string //redis.Scan(values, &v1) //fmt.Println(v1)//释放资源
defer c.Close()var Vr1 Vr
var Vr2 VrVr1.name = "跪射俑"
Vr1.vrurl = "http:" Vr1.belong = "一号坑" Vr1.vrid = 1Vr2.name = "铜车马俑"
Vr2.vrurl = "http:" Vr2.belong = "展览厅" Vr2.vrid = 2/* 打印 Book1 信息 */
/* fmt.Printf("vr 1 title : %s\n", Vr1.name) fmt.Printf("vr 1 author : %s\n", Vr1.belong) fmt.Printf("vr 2 subject : %s\n", Vr2.name) fmt.Printf("vr 2 book_id : %d\n", Vr2.belong) */// 数据库操作
db, err := sql.Open("mysql", "root:root@/ai?charset=utf8") checkErr(err) //查询数据 rows, err := db.Query("SELECT * FROM users") checkErr(err)for rows.Next() {
var uid int var name string var age string var sex string err = rows.Scan(&uid, &name, &age, &sex) checkErr(err) fmt.Println(name) fmt.Println(age) fmt.Println(sex)}
fmt.Println("正在启动WEB服务...")
var mux *routes.RouteMux = routes.New() mux.Get("/user/:uid", getuser) mux.Get("/user/:uid/:age", getuserAndAge) mux.Post("/user/:uid", edituser)//http.Handle("/", mux)
http.ListenAndServe(":8088", mux) fmt.Println("服务已停止")}
func checkErr(err error) {
if err != nil { panic(err) } }