首页 > 解决方案 > https://github.com/seehuhn/fortuna 的问题,或者我误解了 Golang

问题描述

我正在尝试将https://github.com/seehuhn/fortuna用于 Golang 并将其实现到 API 中。

我遇到的问题是,当我分配 *fortuna.Accumulator 并将其分配给我的 App 结构时,我无法在创建它的函数体之外使用它。

请参见下面的示例。

type App struct {
    Config config.Config
    RNG    *fortuna.Accumulator
    Sink   chan<- time.Time
}


func New(cfg config.Config) *App {
    var acc, err = fortuna.NewRNG(cfg.SeedFileName)
    if err != nil {
        panic("cannot initialise the RNG: " + err.Error())
    }
    defer acc.Close()

app := App{Config: cfg, RNG: acc, Sink: sink}
/// if i use app.RNG.Uint64() <---  using this here works correctly

    return &app

package main

import (

    "******/*******/app"

)

func main() {

    app := app.New(cfg)
    app.RNG.Uint64() <--- this causes Generator not seeded yet error

}

标签: go

解决方案


所以你推迟通话

acc.Close()

女巫有效地杀死了RNG。


推荐阅读