参考链接
首先安装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
- 对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()
}
- 对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"))
}
这篇博客介绍了如何在Windows和Linux环境下配置Go环境,接着讲解了Gin与Iris两个Web框架的使用,并展示了在浏览器端启动服务器的效果。此外,还详细探讨了如何使用redisgo库进行Redis的set和hash操作。
4461

被折叠的 条评论
为什么被折叠?



