golang-20606_1.beego web项目开发笔记

1.session
    1)开启session:
    beego.BConfig.WebConfig.Session.SessionOn = true // 开启session
    2)获取session属性值
    authgroup := models.AuthGroup{Group: group,AuthId: auth_id}
    adminsession := c.GetSession("admin")
    fmt.Println(adminsession.(models.Admin).AdminAid)

2.request
    1)获取请求方式
    c.Ctx.Request.Method
    
3.表单
    1)普通int,string类型
    c.GetString("mobile")
    2)数组形式数据
    input := c.Input()
    authgroup.AuthId = strings.Join(input["auth_id[]"], ",")
    3)文件类型
    c.Ctx.Request.FormFile("file")
    
4.数据类型转换
    1)查看数据类型
    reflect.TypeOf(v1)
    2)数组转成字符串
    authgroup.AuthId = strings.Join(input["auth_id[]"], ",")
    3)字符串转成数组
        a)string函数方式:
        s := strings.Split(oldauthgroup.AuthId, ",")
        b)json方式:
        str := "["+authgroup.AuthId+"]"
        var ints []int
        json.Unmarshal([]byte(str), &ints)
        
    
5.json操作
    1)将字符串反解析为结构体
    s := `{"id": 1, "name": "wxnacy"}`
    var user User
    json.Unmarshal([]byte(s), &user)

    2)将字符串反解析为字典
    var d map[string]interface{}
    json.Unmarshal([]byte(s), &d)
    
    3)将字符串反解析为数组
    s = `[1, 2, 3, 4]`
    var a []int
    json.Unmarshal([]byte(s), &a)
    4)将结构体解析为字符串
    b, e := json.Marshal(user)
    
6.数组、切片操作
    1)获取两个数组、切片差集
    for i := 0; i < len(s); i++ {
        for _, v2 := range data4 {
            if s[i] == v2{
                s = append(s[:i], s[i+1:]...)
            }
        }
    }


7.无限分类
    func (c *AuthController) getChild(pid int, auths []*models.Auth)[]*TreeList{
        treeList := []*TreeList{}
        for _, v := range auths{
            if v.Pid == pid{
                child := c.getChild(v.Id,auths)
                node := &TreeList{
                    Id:         v.Id,
                    Attr:       v.Attr,
                    Annotation: v.Annotation,
                    Pid:        v.Pid,
                    ShowClass:  v.ShowClass,
                    Level:      v.Level,
                    Status:     v.Status,
                }
                node.Children = child
                treeList = append(treeList, node)
            }
        }
        return treeList
    }


8.orm的使用
    1)多数据库注册
    orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/device_manage?charset=utf8", 30)
    orm.RegisterDataBase("log", "mysql", "root:root@tcp(127.0.0.1:3306)/device_manage_log?charset=utf8", 30)
    2)开启调试模式
    orm.Debug = true
    3)数据库切换
    o.Using("log")
    
9.文件处理
    1)上传
    file, h, err := c.Ctx.Request.FormFile("file")
        if err != nil {
    }
    defer file.Close()
    newname := fmt.Sprintf("%v%s", time.Now().Unix(), path.Ext(h.Filename))
    tofilepath := path.Join("static/upload/", newname)
    f, err := os.OpenFile(tofilepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
    if err != nil {
        fmt.Println("open excel error,",err.Error())
    }
    defer f.Close()
    io.Copy(f, file)
    
    2)excel读取
    xlsx,err:=excelize.OpenFile(tofilepath)
    if err !=nil{
        fmt.Println("open excel error,",err.Error())
        s.Exit(1)
    }
    rows,err:=xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
    result:= make([]string,0)
    for _,row:=range rows{
        fmt.Println(row)
        result = append(result,row[0])
    }
   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值