dongzhukuai8177 2019-06-18 06:36
浏览 34

从Golang中的结构更新值

I am struggling updating a field coming from Gorm. I am loading all carousels from the database, and have a ticker that checks the field "LastRun", and I want to set a new time.Now() value when it is run.

For now, I only need the loaded struct to be updated, so I know this does not write the changes to the DB at this moment.

How do I update the field carousel.LastRun in func Sequencer() in this example? It keeps having the old value from the DB whatever I do...

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "sync"
    "time"
)

var (
    db *gorm.DB
    wg = &sync.WaitGroup{}
)

type Carousel struct {
    gorm.Model
    Name        string
    Description string
    Duration    uint
    LastRun     time.Time
    Index       uint8
    State       State
}

type State struct {
    Type string
}

func main() {
    path := "pkg/database/database.db"
    db, err := gorm.Open("sqlite3", path)
    if err != nil {
        panic("failed to connect database")
    }
    defer db.Close()

    db.AutoMigrate(&Carousel{})

    var carousels []Carousel
    db.Find(&carousels)
    wg.Add(1)
    Sequencer(&carousels)
    wg.Wait()
}

func Sequencer(carousels *[]Carousel) {

    ticker := time.NewTicker(1000 * time.Millisecond)
    for range ticker.C {
        for _, carousel := range *carousels {
            next := carousel.LastRun.Add(time.Millisecond * time.Duration(carousel.Duration))
            if next.Sub(time.Now()) <= 0 {
                fmt.Println("Carousel: ", carousel.Name, "Last run: ", time.Since(carousel.LastRun))
                carousel.LastRun = time.Now()
                /* How do I update the carousel.LastRun ? */
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dpxyfa4718 2019-06-18 07:27
    关注

    To update the carouselstruct, you can do this:

    func Sequencer(carousels []*Carousel) {
    
    ticker := time.NewTicker(1000 * time.Millisecond)
    for range ticker.C {
        for i, _ := range carousels {
            carousel = carousels[i]
            next := carousel.LastRun.Add(time.Millisecond * time.Duration(carousel.Duration))
            if next.Sub(time.Now()) <= 0 {
                fmt.Println("Carousel: ", carousel.Name, "Last run: ", time.Since(carousel.LastRun))
                carousel.LastRun = time.Now()
            }
        }
      }
    }
    

    When using range, the value used(for the carousel var in your case) is a copy of the element in the slice. Hence, even by updating it, it will not update the element in the actual list.

    To do so, you need to access the index of the slice you need to update and then perform your changes.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c