Gin和Iris等Web框架及redis与go的连接使用

这篇博客介绍了如何在Windows和Linux环境下配置Go环境,接着讲解了Gin与Iris两个Web框架的使用,并展示了在浏览器端启动服务器的效果。此外,还详细探讨了如何使用redisgo库进行Redis的set和hash操作。

参考链接
首先安装go
然后配置环境变量

Windows

修改环境变量和网络代理
前面两句相当于修改环境变量中的参数,然后才能下载。
版本为go1.13以后似乎才有效

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go get -u github.com/gin-gonic/gin
go get github.com/kataras/iris/

Linux

export是linux下的命令

export GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go get -u github.com/gin-gonic/gin
go get github.com/kataras/iris/

Gin服务器

package main

import ("github.com/gin-gonic/gin"
	"log"
)

func main() {
	r := gin.Default()
	r.GET("/hello", func(context *gin.Context) {
		context.Writer.Write([]byte("hello gin!"))
	})

	if err:=r.Run(":9898"); err != nil{
		log.Fatal(err.Error())
	}
}

浏览器端输入

http://localhost:9898/hello

就可以看到

hello gin!

redisgo

  1. 对set的操作
package main
import (
	"fmt"
	"github.com/garyburd/redigo/redis"
)

func main() {
	//连接服务器
	conn, err := redis.Dial("tcp","127.0.0.1:6379")
	if err != nil {
		fmt.Println("redis.Dial err=", err)
		return
	}
	//连接成功
	fmt.Println("connect success!")
	//set操作
	_,err = conn.Do("set","name","ming")
	if err!=nil{
		fmt.Println("set err= ",err)
		return
	}
	fmt.Println("operation success!")
	//get操作
	r, err := redis.String(conn.Do("Get","name"))
	if err!= nil {
		fmt.Println("get err=", err)
		return
	}
	fmt.Println(r)
	defer conn.Close()
}


  1. 对hash的操作
package main

import ("fmt"
"github.com/garyburd/redigo/redis"
)

func main() {
	//连接redis服务器 本地必须事先启动redis服务器
	conn, err := redis.Dial("tcp","127.0.0.1:6379")
	if err != nil {
		fmt.Println("redis.Dial err=", err)
		return
	}
	//向redis中写入数据 HSet
	_,err = conn.Do("HSet","user1","age",18)
	if err != nil{
		fmt.Println("set err=",err)
		return
	}
	//向redis中写入数据 HSet
	_,err = conn.Do("HSet","user1","name","yaojun")
	if err != nil{
		fmt.Println("set err=",err)
		return
	}
	//从redis中获取数据
	var r string
	r,err = redis.String(conn.Do("HGet","user1","name",))
	if err != nil{
		fmt.Println("get err=",err)
		return
	}
	fmt.Println("name = ",r)
	var a int
	a,err = redis.Int(conn.Do("HGet","user1","age",))
	if err != nil{
		fmt.Println("get err=",err)
		return
	}
	fmt.Println("age = ",a)
	defer conn.Close()
}

Iris服务器

package main

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/context"
)

func main() {
	app := iris.New() // 实例一个iris对象

	/*
	app.Get("/hello", func(ctx iris.Context) {
		//获取路径
		path := ctx.Path()
		app.Logger().Info(path)
		//获取名字
		username := ctx.URLParam("username")
		app.Logger().Info(username)
		//密码
		pwd := ctx.URLParam("pwd")
		app.Logger().Info(pwd)
		//返回html 自动渲染 http://localhost:8080/hello?username=yaojun&pwd=helloworld
		ctx.HTML("<h1>"+username+","+pwd+"</h1>")
	})
	*/
	app.Post("login", func(ctx context.Context){
		path := ctx.Path()
		app.Logger().Info("请求url:",path)
		//postValue获取post提交的表单数据
		name := ctx.PostValue("name")
		age:= ctx.PostValue("age")
		ctx.HTML("<h1>"+name+" , "+age+"</h1>")
		//ctx.JSON(iris,Map{"massage":"hello"})
		//读取浏览器传来的json 使用readJSON()
		//读取浏览器传来的XML  使用readXML()
	})
	app.Run(iris.Addr(":8080"),iris.WithCharset("UTF-8"))

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值