1、golang使用redis
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/garyburd/redigo/redis"
)
type User struct {
UserId string `json:"user_id"`
Openid string `json:"openid"`
}
var (
Pool *redis.Pool
)
func init() {
Pool = newPool(
"127.0.0.1:6379",
50,
240,
)
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
conn := GetRedis()
defer conn.Close()
ret, err := redis.Values(conn.Do("HMGET", "localhost_banma:idiom_master:user:111", "user_id", "openid"))
if err != nil {
fmt.Println(err)
}
if ret == nil {
fmt.Println("get redis values return nil")
}
u := &User{}
_, err = redis.Scan(ret, &u.UserId, &u.Openid)
fmt.Println(u.UserId)
fmt.Println("ok")
}
//create a redis pool
func newPool(server string, maxIdle, idleTimeout int) *redis.Pool {
return &redis.Pool{
MaxIdle: maxIdle,
IdleTimeout: time.Duration(idleTimeout) * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
if _, err := c.Do("PING"); err != nil {
c.Close()
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
// _, err := c.Do("PING")
return nil
},
}
}
func GetRedis() redis.Conn {
return Pool.Get()
}
func main() {
http.HandleFunc("/test", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
2、golang 使用mysql
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type Wxgame struct {
Name string `json:"name"`
App string `json:"app"`
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
db, err := GetMySQL()
if err != nil {
fmt.Println(err)
}
defer db.Close()
rows, err := db.Query("SELECT name,app_name FROM wx_game")
if err != nil {
fmt.Println(err)
}
var res []*Wxgame
for rows.Next() {
g := new(Wxgame)
err = rows.Scan(&g.Name, &g.App)
res = append(res, g)
}
data, _ := json.Marshal(res)
fmt.Fprintf(w, string(data)) //这个写入到w的是输出到客户端的
}
//获取mysql连接
func GetMySQL() (db *sql.DB, err error) {
db, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/wxgame")
if err != nil {
return
}
err = db.Ping()
return
}
func main() {
http.HandleFunc("/test", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
3、golang 使用mongo
package main
import (
"fmt"
"log"
"net/http"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Name string `json:"name"`
Password string `json:"password"`
City string `json:"city"`
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
session := GetMongo()
defer session.Close()
collection := session.DB("video").C("users")
user := User{}
err := collection.Find(bson.M{"name": "杨六"}).One(&user)
if err != nil {
fmt.Println(err)
}
fmt.Println(user)
}
func GetMongo() *mgo.Session {
session, err := mgo.DialWithTimeout("127.0.0.1", 10*time.Second)
if err != nil {
return nil
}
session.SetPoolLimit(4096)
session.SetMode(mgo.SecondaryPreferred, true)
safe := &mgo.Safe{
WTimeout: 0,
J: false,
}
safe.W = 1
session.SetSafe(safe)
session.SetSocketTimeout(600 * time.Second)
return session
}
func main() {
http.HandleFunc("/test", sayhelloName) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}