简体   繁体   中英

go fiber slice append changes all items

seems like go slice append (string) changes all items when I render it to the template (also when I log the slice to the terminal) I think this is a golang thing but I am not sure with Django template

Updated the code, but I still have the same problem with mutex and regular Html template

package main

import (
    "log"
    "sync"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html"
)

func main() {
    engine := html.New("./views", ".html")
    app := fiber.New(fiber.Config{
        Views: engine,
    })

    type Whatever struct {
        Whatever string `json:"whatever" form:"whatever"`
    }
    var (
        itemsMu sync.RWMutex
        items   = []string{}
    )

    app.Get("/", func(c *fiber.Ctx) error {
        itemsMu.RLock()
        defer itemsMu.RUnlock()
        return c.Render("index", fiber.Map{
            "Title": "Hello, World!",
            "Slice": items,
        })
    })

    app.Post("/", func(c *fiber.Ctx) error {
        w := new(Whatever)
        if err := c.BodyParser(w); err != nil {
            return err
        }
        itemsMu.Lock()
        items = append(items, w.Whatever)
        log.Println(items)
        itemsMu.Unlock()
        return c.Redirect("/")
    })
    log.Fatal(app.Listen(":3000"))
}

HTTP requests are served on different goroutines. Variables scoped outside of requests (such as your items ) that are accessed from requests must be synchronized.

Accessing (writing) writing variables from different goroutines is a data race and undefined behaviour.

Add proper synchronization, then see the result.

For example:

var (
    itemsMu sync.RWMutex
    items = []string{}
) 

app.Get("/", func(c *fiber.Ctx) error {
    itemsMu.RLock()
    defer itemsMu.RUnlock()
    return c.Render("index", fiber.Map{
        "Title": "Hello, World!",
        "slice": items,
    })
})

app.Post("/", func(c *fiber.Ctx) error {
    w := new(Whatever)
    if err := c.BodyParser(w); err != nil {
        return err
    }
    itemsMu.Lock()
    items = append(items, w.Whatever)
    itemsMu.Unlock()
    return c.Redirect("/")
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM