Monday, May 1, 2023
HomeGolangShop in Go memory like localStorage Javascript? - Obtaining Assistance

Shop in Go memory like localStorage Javascript? – Obtaining Assistance

I have a semi-static food selection that ought to pack from a data source.

To make the lots of food selection much faster I question if there is a method to save this food selections as some kind of worldwide variables? Consistent or otherwise.

You just require to develop a struct to map your information. Additionally you can utilize a basic cache collection like GitHub – patrickmn/go-cache: An in-memory secret: worth store/cache (comparable to Memcached) collection for Go, appropriate for single-machine applications.

I do not believe you require an outside dependence for something this simple. Produce an international variable of some kind, guard it with a mutex so it’s thread-safe, after that upgrade it based upon some kind of period. Something like:

// AppMenu shops the worldwide application food selection things.
kind AppMenu struct {
Products [] string.

var (.
muAppMenu = && sync.RWMutex {}// Guards 'appMenu'.
appMenu AppMenu.

// GetAppMenu returns the existing application food selection.
func GetAppMenu() AppMenu {
food selection:= appMenu.
return food selection.

// SetAppMenu establishes the application food selection.
func SetAppMenu( food selection AppMenu) {
appMenu = food selection.

You might after that someplace in your primary feature rotate up a goroutine to upgrade your food selection thing home occasionally:

// Context for our goroutine.
ctx, terminate:= context.WithCancel( context.Background()).
delay terminate().
// Rotate up goroutine to upgrade application food selection things every 30 secs.
go func() {
t:= time.NewTicker( 30 * time.Second).
delay t.Stop().
for {
pick {
situation <


Most Popular

Recent Comments