好久没有写go,今天像往常一样拉包,尴尬的被墙了,使用goproxy 一样拉不下来,通过巨佬的指导,直接go mod 爽歪歪!!
拉包拉不下来,痛苦,难受,影响效率
拉包a.png
go mod 只需第一步 go mod init (形成一个go.mod)第二步 go tidy(形成go.sum解决乱七八糟的依赖) 第三步 go build 就可以了
gomod.png
第一次接触iris 这个号称宇宙最快的框架 ,随便写点练练手了
/*
*导入 curl --data "users=zhangsan,wangwu" http://localhost:8080/import
*/
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
//人数
var userList []string
//定义一个controller
type LotteryController struct {
Ctx iris.Context
}
//实现 初始化
func newApp() *iris.Application {
app := iris.New()
mvc.New(app.Party("/")).Handle(&LotteryController{})
return app
}
//首页
//GET http://localhost:8080
func (l *LotteryController) Get() string {
count := len(userList)
return fmt.Sprintf("参与测试的人数:%d", count)
}
//导入用户名单
//POST http://localhost:8080/import
func (l *LotteryController) PostImport() string {
strUser := l.Ctx.FormValue("users")
users := strings.Split(strUser, ",")
countFirst := len(userList)
for _, u := range users {
u = strings.TrimSpace(u)
if len(u) > 0 {
userList = append(userList, u)
}
}
countLast := len(userList)
return fmt.Sprintf("活动开始前的抽奖用户数量:%d,成功导入的用户数量%d\n", countFirst, (countLast - countFirst))
}
//抽奖开始 GET http://localhost:8080/lucky
func (l *LotteryController) GetLucky() string {
count := len(userList)
//有用户
if count > 1 {
//获取当前时间戳
nowSeed := time.Now().UnixNano()
//生成一个0-count之间的一个随机数 Int31n返回一个取值范围在[0,n)的伪随机int32值,
index := rand.New(rand.NewSource(nowSeed)).Int31n(int32(count))
//从userlist里面取到一个用户 并对userlist更新 将取到的更新掉
user := userList[index]
userList = append(userList[0:index], userList[index+1:]...)
return fmt.Sprintf("当前用户:%s,剩余用户数:%d\n", user, count-1)
} else if count == 1 {
user := userList[0]
return fmt.Sprintf("当前用户:%s,剩余用户数:%d\n", user, count-1)
} else {
return fmt.Sprintf("已经没有参与用户")
}
}
func main() {
app := newApp()
userList = []string{}
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8"))
}
一个简单的抽奖,今天跑通功能第一步,剩下的慢慢优化代码了,每天进步一点点.